Presentation is loading. Please wait.

Presentation is loading. Please wait.

Texas State Technical College DISCOVER! Union and Union All Bringing things together.

Similar presentations


Presentation on theme: "Texas State Technical College DISCOVER! Union and Union All Bringing things together."— Presentation transcript:

1 Texas State Technical College DISCOVER! Union and Union All Bringing things together.

2 Texas State Technical College DISCOVER! Overview Sometimes it is necessary to take two completely separate queries and merge the results together. Oracle SQL includes 2 commands for merging or combining the results from two or more queries. UnionUnion Union AllUnion All In order to be able to merge queries, each query being merged must contain the same number of return fields/values in the SELECT body of the statement. Union and Union All

3 Texas State Technical College DISCOVER! Overview A Union between two sets is essentially an OR between the components. Thus, in a sense, the UNION of two statements is similar to using an OR between the results. Union and Union All

4 Texas State Technical College DISCOVER! Union The UNION keyword combines the results of two or more queries and ELIMINATES DUPLICATES. SyntaxSELECT…UNIONSELECT…;Note There is only one closing semicolon. The Union keyword is like a conjunction that joins two sentences. Union and Union All

5 Texas State Technical College DISCOVER! Union All The UNION ALL keyword combines the results of two or more queries and DOES NOT eliminate duplicates. SyntaxSELECT… UNION ALL SELECT…;Note There is only one closing semicolon. The Union keyword is like a conjunction that joins two sentences. Union and Union All

6 Texas State Technical College DISCOVER! Union Example… The following statement displays the identifier of patients who have an appointment today OR who are using medicare. It suppresses any duplicates. SELECT p_id FROM patient WHERE insurance = ‘Medicare’ UNION SELECT p_id FROM appointment WHERE a_date = sysdate ; Union and Union All

7 Texas State Technical College DISCOVER! Union Example… If we wished to display additional information from one table, we would need to guarantee that the number of returned fields is the same. SELECT p_id, p_last, p_first FROM patient WHERE insurance = ‘Medicare’ UNION SELECT p_id, NULL, NULL FROM appointment WHERE a_date = sysdate ; The appointment table would not store patient first and last name. Thus we use NULL in their place in the second statement. When doing this, there will be no duplicates between both statements. Union and Union All

8 Texas State Technical College DISCOVER! Union All Example… The Union All statement is exactly the same; however, duplicates between the two queries will be displayed. Notice that DISTINCTs do not help because the duplicates across queries would still be displayed. SELECT DISTINCT p_id, p_last, p_first FROM patient WHERE insurance = ‘Medicare’ UNION ALL SELECT DISTINCT p_id, NULL, NULL FROM appointment WHERE a_date = sysdate ; Union and Union All

9 Texas State Technical College DISCOVER! History Before the introduction of the outer join capability in SQL, Union All was utilized to perform outer joins. This displays all customers and then any orders they may have. SELECT c.c_id, c.c_last, c.c_first, o.o_id, o.o_date FROM customer c JOIN orders o ON (c.c_id = o.c_id) UNION ALL SELECT c_id, c_last, c_first, NULL, NULL FROMcustomer; Union and Union All

10 Texas State Technical College DISCOVER! In Summary… The UNION and UNION ALL keywords are used to merge the results of two or more queries. UNION eliminates duplicates that arise BETWEEN the two queries. DISTINCT will only eliminate duplicates within a query. UNION ALL does not eliminate duplicates between the queries. The SELECT body of all statements in a UNION or UNION ALL must contain the same number of values or fields. Union and Union All


Download ppt "Texas State Technical College DISCOVER! Union and Union All Bringing things together."

Similar presentations


Ads by Google