UNION, INTERSECT and EXCEPT
Joins put tables side by side. Set operations stack them on top of each other: all rows from both, the rows they share, or the rows only one of them has. They are the quickest way to compare two copies of the same data.
Foundations · SQL for data engineers · Silver · Structure, sets and dates, lesson 2 of 3
On this page
Read first: Inner and outer joins (skip if you know it)
In plain words first
Think of each query result as a list of names. Set operations combine two lists:
UNION ALL
UNION
INTERSECT
EXCEPT
The examples compare customers who ordered early in the week (1 to 2 March) with those who ordered late (5 to 6 March).
UNION ALL and UNION
Input: orders
| order_id | customer_id | city | amount | order_date |
|---|---|---|---|---|
| 201 | 1 | Pune | 420 | 2026-03-01 |
| 202 | 2 | Delhi | 650 | 2026-03-01 |
| 203 | 1 | Pune | 300 | 2026-03-02 |
| 204 | 3 | Pune | 540 | 2026-03-02 |
| 205 | 2 | Delhi | 280 | 2026-03-04 |
| 206 | 4 | Pune | 540 | 2026-03-04 |
| 207 | 5 | Mumbai | 390 | 2026-03-05 |
| 208 | 1 | Pune | 180 | 2026-03-05 |
| 209 | 2 | Delhi | 650 | 2026-03-06 |
| 210 | 5 | Mumbai | 210 | 2026-03-06 |
SELECT customer_id FROM orders WHERE order_date <= '2026-03-02' UNION ALL SELECT customer_id FROM orders WHERE order_date >= '2026-03-05'
Eight rows: four early orders and four late ones, with customer 1 three times. UNION removes the duplicates:
Input: orders
| order_id | customer_id | city | amount | order_date |
|---|---|---|---|---|
| 201 | 1 | Pune | 420 | 2026-03-01 |
| 202 | 2 | Delhi | 650 | 2026-03-01 |
| 203 | 1 | Pune | 300 | 2026-03-02 |
| 204 | 3 | Pune | 540 | 2026-03-02 |
| 205 | 2 | Delhi | 280 | 2026-03-04 |
| 206 | 4 | Pune | 540 | 2026-03-04 |
| 207 | 5 | Mumbai | 390 | 2026-03-05 |
| 208 | 1 | Pune | 180 | 2026-03-05 |
| 209 | 2 | Delhi | 650 | 2026-03-06 |
| 210 | 5 | Mumbai | 210 | 2026-03-06 |
SELECT customer_id FROM orders WHERE order_date <= '2026-03-02' UNION SELECT customer_id FROM orders WHERE order_date >= '2026-03-05' ORDER BY customer_id
An ORDER BY at the end sorts the combined result, not the last query. To label where each row came from, add a constant column:
Input: orders
| order_id | customer_id | city | amount | order_date |
|---|---|---|---|---|
| 201 | 1 | Pune | 420 | 2026-03-01 |
| 202 | 2 | Delhi | 650 | 2026-03-01 |
| 203 | 1 | Pune | 300 | 2026-03-02 |
| 204 | 3 | Pune | 540 | 2026-03-02 |
| 205 | 2 | Delhi | 280 | 2026-03-04 |
| 206 | 4 | Pune | 540 | 2026-03-04 |
| 207 | 5 | Mumbai | 390 | 2026-03-05 |
| 208 | 1 | Pune | 180 | 2026-03-05 |
| 209 | 2 | Delhi | 650 | 2026-03-06 |
| 210 | 5 | Mumbai | 210 | 2026-03-06 |
Input: payments
| order_id | amount |
|---|---|
| 201 | 420 |
| 202 | 650 |
| 203 | 300 |
| 204 | 540 |
| 205 | 280 |
| 206 | 450 |
| 207 | 390 |
| 209 | 650 |
| 211 | 120 |
SELECT 'orders' AS source, count(*) AS row_count FROM orders UNION ALL SELECT 'payments', count(*) FROM payments
INTERSECT and EXCEPT
Input: orders
| order_id | customer_id | city | amount | order_date |
|---|---|---|---|---|
| 201 | 1 | Pune | 420 | 2026-03-01 |
| 202 | 2 | Delhi | 650 | 2026-03-01 |
| 203 | 1 | Pune | 300 | 2026-03-02 |
| 204 | 3 | Pune | 540 | 2026-03-02 |
| 205 | 2 | Delhi | 280 | 2026-03-04 |
| 206 | 4 | Pune | 540 | 2026-03-04 |
| 207 | 5 | Mumbai | 390 | 2026-03-05 |
| 208 | 1 | Pune | 180 | 2026-03-05 |
| 209 | 2 | Delhi | 650 | 2026-03-06 |
| 210 | 5 | Mumbai | 210 | 2026-03-06 |
SELECT customer_id FROM orders WHERE order_date <= '2026-03-02' INTERSECT SELECT customer_id FROM orders WHERE order_date >= '2026-03-05'
Input: orders
| order_id | customer_id | city | amount | order_date |
|---|---|---|---|---|
| 201 | 1 | Pune | 420 | 2026-03-01 |
| 202 | 2 | Delhi | 650 | 2026-03-01 |
| 203 | 1 | Pune | 300 | 2026-03-02 |
| 204 | 3 | Pune | 540 | 2026-03-02 |
| 205 | 2 | Delhi | 280 | 2026-03-04 |
| 206 | 4 | Pune | 540 | 2026-03-04 |
| 207 | 5 | Mumbai | 390 | 2026-03-05 |
| 208 | 1 | Pune | 180 | 2026-03-05 |
| 209 | 2 | Delhi | 650 | 2026-03-06 |
| 210 | 5 | Mumbai | 210 | 2026-03-06 |
SELECT customer_id FROM orders WHERE order_date <= '2026-03-02' EXCEPT SELECT customer_id FROM orders WHERE order_date >= '2026-03-04'
Meera (customer 3) ordered on the 2nd and not after. EXCEPT is one-directional: A EXCEPT B is not the same as B EXCEPT A. Spark also accepts MINUS as another name for EXCEPT.
INTERSECT and EXCEPT also remove duplicates. Their ALL versions keep count: if a row appears 3 times on the left and once on the right, EXCEPT ALL keeps it twice.
The rules: same columns, matched by position
Both queries must return the same number of columns, and the columns are matched by position, not by name. The result takes its column names from the first query. Swap two columns of the same type and nothing fails: the values are silently mixed up.
SELECT order_id, customer_id FROM orders WHERE order_id = 201 UNION ALL SELECT customer_id, order_id FROM orders WHERE order_id = 202
Output
order_id | customer_id 201 | 1 2 | 202
The second row claims order 2 belongs to customer 202. Always list the columns in the same order in every part, and never use SELECT * on both sides of a set operation: a column added to one table later shifts everything.
NULLs count as equal here
In a join, NULLNULL: A missing or unknown value. It is not 0 or an empty string, and any comparison with it is unknown instead of true or false. Learn more → never matches NULL. In set operations, two NULLs are treated as the same value, so a row with a NULL can be found in both inputs:
SELECT * FROM VALUES (1, NULL), (2, 'x') AS t(id, note) INTERSECT SELECT * FROM VALUES (1, NULL), (2, 'y') AS t(id, note)
That makes EXCEPT a NULL-safe way to compare two tables, where a join on every column would quietly miss rows with NULLs.
Reconciling two sources
The payments table is what the payment provider says it collected. Does it agree with orders? EXCEPT in both directions answers it:
Input: orders
| order_id | customer_id | city | amount | order_date |
|---|---|---|---|---|
| 201 | 1 | Pune | 420 | 2026-03-01 |
| 202 | 2 | Delhi | 650 | 2026-03-01 |
| 203 | 1 | Pune | 300 | 2026-03-02 |
| 204 | 3 | Pune | 540 | 2026-03-02 |
| 205 | 2 | Delhi | 280 | 2026-03-04 |
| 206 | 4 | Pune | 540 | 2026-03-04 |
| 207 | 5 | Mumbai | 390 | 2026-03-05 |
| 208 | 1 | Pune | 180 | 2026-03-05 |
| 209 | 2 | Delhi | 650 | 2026-03-06 |
| 210 | 5 | Mumbai | 210 | 2026-03-06 |
Input: payments
| order_id | amount |
|---|---|
| 201 | 420 |
| 202 | 650 |
| 203 | 300 |
| 204 | 540 |
| 205 | 280 |
| 206 | 450 |
| 207 | 390 |
| 209 | 650 |
| 211 | 120 |
SELECT 'missing in payments' AS problem, order_id, amount FROM (SELECT order_id, amount FROM orders EXCEPT SELECT order_id, amount FROM payments) UNION ALL SELECT 'missing in orders', order_id, amount FROM (SELECT order_id, amount FROM payments EXCEPT SELECT order_id, amount FROM orders) ORDER BY order_id, problem
Three kinds of difference show up: 208 and 210 were never paid, 211 was paid but has no order, and 206 appears on both sides because the amounts differ (540 against 450). When the result is empty, the two tables hold exactly the same rows.
Common mistakes
- UNION when UNION ALL would do — UNION removes duplicates with an expensive shuffle. If the parts cannot overlap, use UNION ALL.
- Columns in a different order in each part — They are matched by position, so values land in the wrong column with no error. List columns in the same order everywhere.
- SELECT * in a set operation — A new column in one table shifts or breaks the match. Name the columns.
- Checking only one direction — A EXCEPT B finds what B lacks, not what A lacks. Reconcile in both directions.
Key takeaways
- UNION ALL stacks rows; UNION also removes duplicates, at a cost.
- INTERSECT keeps rows in both; EXCEPT keeps rows in the first and not the second.
- Columns are matched by position, and names come from the first query.
- Set operations treat NULLs as equal, unlike joins.
- Reconcile two sources with EXCEPT in both directions.
Interview prep
THE QUESTION
"Two tables should hold the same data. How do you check, in SQL, that they do?"
(A EXCEPT B) UNION ALL (B EXCEPT A), labelling each side. Empty means the same set of rows; EXCEPT treats NULLs as equal, so rows with NULLs compare correctly, which a join on every column would not. Two caveats: EXCEPT removes duplicates, so use EXCEPT ALL (or compare counts per key) if duplicate rows matter; and for very wide tables, comparing a hash of each row first is cheaper.Avoid: "Join the two tables on every column and count the matches." Rows with a NULL in any column never match in a join, so they show up as differences that are not real, or are missed entirely.
Follow-ups · answer out loud, then open
Follow-up"UNION or UNION ALL: which is faster, and why?"
Scenario"Daily files from two vendors have the same columns in a different order. You UNION ALL them and totals look wrong."
Trap"A EXCEPT B returns nothing. Are the tables identical?"
Check yourself
4 questionsQuery A returns 4 rows and query B 4 rows, 2 of them identical to rows of A. How many rows does UNION ALL return?
Show the answer
8. UNION ALL keeps every row.
How are the columns of the two queries matched?
Show the answer
By position. Position only; names come from the first query.
Does (1, NULL) INTERSECT (1, NULL) return a row?
Show the answer
Yes. Set operations treat two NULLs as equal.
A EXCEPT B is empty. What do you know?
Show the answer
Every row of A is also in B. B may still have rows A lacks: check B EXCEPT A too.
Practise this
Problems that use this, graded on hidden tests
Keep going
Up next · Structure, sets and dates · lesson 3 of 3 · 6 min readDates and date spines
Weeks, months and date arithmetic, half-open ranges, and a calendar that keeps days with no data.
Related lessons
Inner and outer joinsSQL for data engineers · 7 min read
Semi and anti joinsSQL for data engineers · 6 min read
CTEs and subqueries
Previous: CTEs and subqueries
Primary sources: Set operators