Database Tutorial

Course Tutorial Site

Site Admin

Set Operators

without comments

Set operators in SQL are like joins but different. That’s because set operations like the INTERSECT, MINUS, and UNION operators join the results of two queries based on a match, exclusion, or duplicate elimination respectively. A fourth set operator returns the unique rows plus two copies of any duplicate rows, and it is the UNION ALL operator. More or less they’re qualified as follows:

INTERSECT
An INTERSECT operates on two queries that return select lists that share the exact position, number of columns, and data types. An INTERSECT operator returns the rows from both queries that share the same values in all of the select list columns.
MINUS
A MINUS operates like the INTERSECT operator in that both queries must share select lists that match by position, number of columns, and data types. The difference is that the any rows from the first query found in the second query are removed from the result set.
UNION
A UNION acts like a union operator in set mathematics. It returns the unique set of rows found in the collection of both queries. It calculates the distinct set of rows by performing a sort operation that eliminates duplicates. Like the other set operators, the UNION requires that both queries return the same, position, number of columns, and data types.
UNION ALL
A UNION ALL takes all rows returned by one query and adds the rows returned by another query. It does not filter out duplicate rows. It is most useful when both queries return unique sets because it avoids a sorting operation. Like the other set operators, the UNION requires that both queries return the same, position, number of columns, and data types.

The following scrollable list exams the four set operator. They’re defined in the ANSI SQL:89 definition and work with either the Oracle Proprietary outer join or the ANSI SQL:92 style outer joins.

Written by michaelmclaughlin

January 17th, 2010 at 10:28 pm

Posted in

Leave a Reply