Set operators are used in compound queries: queries that combine the results
of two queries. The number of columns selected in both queries must be the
same 。The table below lists the set operators and how to use them.
Operator | Purpose | Example |
---|---|---|
UNION | Returns all rows from either queries; no duplicate rows. | SELECT last_name from emp UNION SELECT first_ name from emp; |
UNION ALL | Returns all rows from either query, including duplicates. | SELECT last_name FROM emp UNION ALL SELECT first_name FROM emp; |
INTERSECT | Returns distinct rows that are returned by both queries. | SELECT last_name FROM emp INTERSECT SELECT first_name FROM emp; |
MINUS | Returns distinct rows that are returned by the first query but not returned by the second. | SELECT last_name FROM emp MINUS SELECT first_ name FROM emp; |