Set Operations: UNION, INTERSECT, EXCEPT

Set operations combine the results of two SELECTs row by row. Where a join attaches columns side by side, a set operation stacks rows top to bottom.

OperationResult
UNIONRows from both sides, duplicates removed
UNION ALLRows from both sides as is (duplicates kept)
INTERSECTRows present on both sides
EXCEPTRows on the left side only (Oracle uses MINUS; EXCEPT is also supported from 21c)

The rules are as follows.

  • Both sides must have the same number of columns, and columns in the same position must have compatible types.
  • The result column names come from the first SELECT.
  • ORDER BY is written once at the very end and sorts the whole result.

UNION ALL and UNION

UNION ALL: duplicates kept

SELECT region FROM customers WHERE cust_id BETWEEN 1 AND 5
UNION ALL
SELECT region FROM customers WHERE cust_id BETWEEN 4 AND 8
ORDER BY region

UNION: duplicates removed

SELECT region FROM customers WHERE cust_id BETWEEN 1 AND 5
UNION
SELECT region FROM customers WHERE cust_id BETWEEN 4 AND 8
ORDER BY region

UNION sorts or hashes the whole result to remove duplicates. When you know the two sides do not overlap, or you need the duplicates, use UNION ALL.

Different aggregates in one result

As long as the column count and types match, you can stack different tables or aggregates. Adding a constant label column shows which side each row came from.

Customer counts per grade and per region in one list

SELECT 'grade' AS kind, grade AS name, COUNT(*) AS cnt FROM customers GROUP BY grade
UNION ALL
SELECT 'region', region, COUNT(*) FROM customers GROUP BY region
ORDER BY kind, name

INTERSECT and EXCEPT

Among customers 101–200, compare the customers who ordered in 2025 with the customers who have a canceled order.

Oracle, PostgreSQL · INTERSECT: both apply

SELECT cust_id FROM orders WHERE cust_id BETWEEN 101 AND 200 AND ord_dt >= DATE '2025-01-01'
INTERSECT
SELECT cust_id FROM orders WHERE cust_id BETWEEN 101 AND 200 AND status = 'CANCELED'
ORDER BY cust_id

SQL Server · INTERSECT: both apply

SELECT cust_id FROM orders WHERE cust_id BETWEEN 101 AND 200 AND ord_dt >= CAST('2025-01-01' AS date)
INTERSECT
SELECT cust_id FROM orders WHERE cust_id BETWEEN 101 AND 200 AND status = 'CANCELED'
ORDER BY cust_id

Oracle, PostgreSQL · EXCEPT: ordered in 2025 but never canceled

SELECT cust_id FROM orders WHERE cust_id BETWEEN 101 AND 200 AND ord_dt >= DATE '2025-01-01'
EXCEPT
SELECT cust_id FROM orders WHERE cust_id BETWEEN 101 AND 200 AND status = 'CANCELED'
ORDER BY cust_id

SQL Server · EXCEPT: ordered in 2025 but never canceled

SELECT cust_id FROM orders WHERE cust_id BETWEEN 101 AND 200 AND ord_dt >= CAST('2025-01-01' AS date)
EXCEPT
SELECT cust_id FROM orders WHERE cust_id BETWEEN 101 AND 200 AND status = 'CANCELED'
ORDER BY cust_id

Like UNION, INTERSECT and EXCEPT remove duplicates from the result. The standard INTERSECT ALL and EXCEPT ALL are supported only by PostgreSQL and Oracle 21c and later.

Set operations and NULL

When comparing for duplicates, set operations treat two NULLs as the same value. This differs from = in WHERE, which treats two NULLs as unknown.

Two NULLs merge into one

SELECT NULLIF(region, region) AS r FROM customers WHERE cust_id = 1
UNION
SELECT NULLIF(region, region) FROM customers WHERE cust_id = 2

How to find rows that exist in only one of two query results is covered in Comparing Two Query Results.