Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practice writing SQL statements Discuss SQL UNION statement.

Similar presentations


Presentation on theme: "Practice writing SQL statements Discuss SQL UNION statement."— Presentation transcript:

1 Practice writing SQL statements Discuss SQL UNION statement.
Agenda for Class (03/09/2006) Practice writing SQL statements Discuss SQL UNION statement. Provide guidelines for testing and debugging SQL.

2 A way to combine the result tables of separate SQL statements.
What is a SQL UNION? A way to combine the result tables of separate SQL statements. Not really a “JOIN” statement. Does not combine underlying tables, as a JOIN statement. Combines result tables into a single result table. Produces a table of the concatenated results from SELECT statements.

3 Example of a UNION statement
Imagine that we have two tables: A table containing our current customers (customer_tbl) and a table containing our past customers (old_customer_tbl). Imagine that we want to have a combined list of the names of both our current and old customers.

4 SELECT cust_name FROM customer_tbl UNION FROM old_customer_tbl; The UNION statement eliminates any items that are duplicated between the two result tables. It checks for duplication on the requested column or columns.

5 Example of a UNION ALL statement
What if you want to see all of the results of the two select statements whether or not there are duplications? SELECT cust_name FROM customer_tbl UNION ALL FROM old_customer_tbl ORDER BY cust_name;

6 Example of an INTERSECT statement
What if you want to look only at those rows that are common to both queries? SELECT cust_name FROM customer_tbl INTERSECT FROM old_customer_tbl ORDER BY cust_name;

7 Example of a MINUS statement
What if you want to see only those rows that are in the current customer_tbl but that aren’t in the old_customer_tbl (past customers)? SELECT cust_name FROM customer_tbl MINUS FROM old_customer_tbl ORDER BY cust_name;

8 Another example of the MINUS
Or maybe you want to see only those rows in the old_customer_tbl that don’t also exist in the current customer_tbl. SELECT cust_name FROM old_customer_tbl MINUS FROM customer_tbl ORDER BY cust_name;

9 Can combine any “like” queries
What if you want to see a list of customers and products together? SELECT cust_name FROM customer_tbl UNION SELECT prod_desc FROM products_tbl ORDER BY 1;

10 Results on previous page are fairly ugly
Results on previous page are fairly ugly. Can be improved by sorting, formatting, and giving a column name. COLUMN custprod heading “Customers and Products” SELECT initcap(cust_name) CustProd FROM customer_tbl UNION SELECT initcap(prod_desc) FROM products_tbl ORDER BY 1;

11 So, what are the rules? Must have the same number of columns in the select lists of the queries that are combined. The columns must be of the same type when compared column-by-column in the select lists of the queries that are combined. The name(s) of the columns are taken from the first query. The ORDER BY clause is best used with positioning rather than the name(s) of columns.

12 IS 475/675 - SQL Union Statement
Debugging SQL Code SQL code design advice: Break the problem down into pieces. DO NOT try to write the entire solution in a single query. Think of dividing the problem into a set of objectives (join tables, eliminate rows, group by) and then write the code that achieves each objective. Use VIEWs to facilitate the division of code into manageable sections. Debugging DO NOT try to write the entire solution in a single query. Write the solution incrementally. Use SQL to help you determine the correct answer so that you know what you should get before writing the actual code. Determine how the code works in each step of the query execution process.


Download ppt "Practice writing SQL statements Discuss SQL UNION statement."

Similar presentations


Ads by Google