Chapter 12 – Set Operators Functions

"The man who doesn't read good books has no advantage over the man who can't read them."

- Mark Twain

Rules of Set Operators

1.Each query will have at least two SELECT Statements separated by a SET Operator

2.SET Operators are UNION, INTERSECT, or MINUS

3.Must specify the same number of columns from the same domain (data type/range)

4.If using Aggregates, both SELECTs much have their own GROUP BY

5.Both SELECTS must have a FROM Clause

6.The First SELECT is used for all ALIAS and FORMAT Statements

7.The Second SELECT will have the ORDER BY statement which must be a number

8.When multiple operators the order of precedence is INTERSECT, UNION, and MINUS

9.Parentheses can change the order of Precedence

10.Duplicate rows are eliminated in the spool unless the ALLkeyword is used

INTERSECT Explained Logically

image

SELECT * FROM Table_Red

INTERSECT

SELECT * FROM Table_Blue ;

In this example, what numbers in the answer set would come from the query above ?

INTERSECT Explained Logically

image

SELECT * FROM Table_Red

INTERSECT

SELECT * FROM Table_Blue ;

3

In this example, only the number 3 was in both tables so they INTERSECT.

UNION Explained Logically

image

SELECT * FROM Table_Red

UNION

SELECT * FROM Table_Blue ;

In this example, what numbers in the answer set would come from the query above ?

UNION Explained Logically

image

SELECT * FROM Table_Red

UNION

SELECT * FROM Table_Blue ;

1   2   3   4   5

Both top and bottom queries run simultaneously, then the two different spools files are merged to eliminate duplicates and place the remaining numbers in the answer set.

UNION ALL Explained Logically

image

SELECT * FROM Table_Red

UNION ALL

SELECT * FROM Table_Blue ;

In this example, what numbers in the answer set would come from the query above?

UNION ALL Explained Logically

image

SELECT * FROM Table_Red

UNION ALL

SELECT * FROM Table_Blue ;

1  2  3  3  4  5

Both top and bottom queries run simultaneously, then the two different spools files are merged together to build the answer set. The ALL prevents eliminating Duplicates.

Minus Explained Logically

image

What will the answer set be? Notice I changed the order of the tables in the query!

Minus Explained Logically

image

SELECT * FROM Table_Blue

MINUS

SELECT * FROM Table_Red ;

4   5

The Top query SELECTED 3, 4, 5 from Table_Blue. From that point on, only 3, 4, 5 at most could come back. The bottom query is run on Table_Red, and if there are any matches, they are not ADDED to the 3, 4, 5 but instead take away either the 3, 4, or 5.

An Equal Amount of Columns in both SELECT List

image

You must have an equal amount of columns in both SELECT lists. This is because data is compared from the two spool files, and duplicates are eliminated. So, for comparison purposes, there must be an equal amount of columns in both queries.

Columns in the SELECT list should be from the same Domain

image

The above query works without error, but no data is returned. There are no First Names that are the same as Department Names. This is like comparing Apples to Oranges. That means they are NOT in the same Domain.

The Top Query handles all Aliases

image

The Top Query is responsible for ALIASING

The Bottom Query does the ORDER BY

image

The Bottom Query is responsible for sorting.

Great Trick: Place your Set Operator in a Derived Table

SELECT E.Employee_No AS MANAGER

,Trim(Last_Name) || ', ' || First_Name as "Name"

FROM     Employee_Table E

INNER JOIN

(SELECT Employee_No

 FROM   Employee_Table

INTERSECT

SELECT Mgr_No

FROM Department_Table) TeraTom

ON E.Employee_No = TeraTom.Employee_No

ORDER BY "Name"

MANAGER

Name

1256349

Harrison, Herbert

1333454

Smith, John

1000234

Smythe, Richard

1121334

Strickling, Cletus

The Derived Table gave us the empno for all managers, and we were able to join it.

UNION Vs UNION ALL

SELECT Department_Name, Dept_No

FROM Department_Table

UNIONALL

SELECT Department_Name, Dept_No

FROM Department_Table

ORDER BY 1;

image

UNION eliminates duplicates, but UNION ALL does not.

A Great Example of how MINUS works

image

SELECT Dept_No as Department_Number

FROM    Department_Table

MINUS

SELECT Dept_No

FROM    Employee_Table

ORDER BY 1 ;

Department_Number

500

This query brought back all Departments without any employees.

USING Multiple SET Operators in a Single Request

image

Above we use multiple SET Operators. They follow the natural Order of Precedence in that UNION is evaluated first, then INTERSECT, and finally MINUS.

Changing the Order of Precedence with Parentheses

image

Above we use multiple SET Operators and Parentheses to change the order of precedence. Above the EXCEPT runs first, then the INTERSECT and lastly, the UNION. The natural Order of Precedence without parentheses is UNION, INTERSECT, and finally EXCEPT or MINUS.

Using UNION ALL for speed in Merging Data Sets

image

This is a clever trick that might help speed things up.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset