Set Operators
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
INTERSECToperates on two queries that return select lists that share the exact position, number of columns, and data types. AnINTERSECToperator returns the rows from both queries that share the same values in all of the select list columns. - MINUS
- A
MINUSoperates like theINTERSECToperator 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
UNIONacts 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, theUNIONrequires that both queries return the same, position, number of columns, and data types. - UNION ALL
- A
UNION ALLtakes 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, theUNIONrequires 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.
SQL Set Operations ↓
Rule(s):
- The top query select list must mirror the bottom query.
- The top query column names must match the bottom query column names.
- The top query must have the same number of columns as the bottom query.
- The top query must match the positional ordering of the bottom query.



