Set Differences & Intersections (INTERSECT, EXCEPT)
Find shared or missing rows between queries with INTERSECT and EXCEPT.
In plain words
INTERSECT keeps what both lists share. EXCEPT keeps what's in the first list but not the second.
Think of it like…
Two guest lists: INTERSECT is the people on both lists; EXCEPT is the people on the first list who aren't on the second.
Where you’ve seen it
Checking a migration copied a table exactly: (SELECT * FROM orders_old EXCEPT SELECT * FROM orders_new) UNION ALL (SELECT * FROM orders_new EXCEPT SELECT * FROM orders_old);. Zero rows means the two tables hold the same rows (ignoring duplicates).
Words you’ll hear
- INTERSECT
- Rows present in both results.
- EXCEPT / MINUS
- Rows in the first result but not in the second.
- ALL variants
- INTERSECT ALL and EXCEPT ALL keep duplicate counts (PostgreSQL).
- Whole-row comparison
- Every selected column must match, and NULL equals NULL.
Visual diagram
Theory & example queries
How they behave
Both queries must return the same number of columns with compatible types, matched by position. INTERSECT keeps rows found in both; EXCEPT keeps rows from the left that have no identical row on the right. By default both remove duplicates; PostgreSQL's INTERSECT ALL and EXCEPT ALL keep multiplicity (if A has a row 3 times and B twice, A EXCEPT ALL B returns it once).
Set operations compare whole rows and consider two NULLs equal, which joins don't (NULL = NULL is never true in ON). That makes EXCEPT a convenient, NULL-safe way to diff two tables with identical shapes. In standard SQL, INTERSECT binds more tightly than UNION and EXCEPT, so use parentheses when combining them.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT user_idFROM ordersWHERE created_at >= '2026-10-01' AND created_at < '2026-11-01'INTERSECTSELECT user_idFROM ordersWHERE created_at >= '2026-11-01' AND created_at < '2026-12-01';PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative repeat purchasers3 sample rows
| user_id |
|---|
| 17 |
| 42 |
| 91 |
Note: Assumes these users placed at least one order in both October and November 2026; INTERSECT removes duplicate user_id values.
Key takeaway: Positional, whole-row, duplicate-removing, NULL-equal.
Set operations vs EXISTS and joins
EXCEPT and NOT EXISTS can answer the same question, but they compare different things. EXCEPT compares every selected column, so to find users who bought in October but not November you select only user_id on both sides. NOT EXISTS compares on chosen keys and can still return other columns from the outer table, such as the user's name and email.
Use set operations for clean list comparisons and table diffs. Use EXISTS / NOT EXISTS (or joins) when you need extra columns, partial matching on some keys, or when the planner handles them better on large data. MySQL supports INTERSECT and EXCEPT only from 8.0.31; before that, use joins or EXISTS.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
| id | country | city | signup_date |
|---|---|---|---|
| 1 | IN | Pune | 2026-10-01 |
| 2 | IN | Delhi | 2026-10-02 |
| 3 | NULL | Mumbai | 2026-10-03 |
SELECT u.id, u.emailFROM users uWHERE EXISTS ( SELECT 1 FROM orders o WHERE o.user_id = u.id AND o.created_at >= '2026-10-01' AND o.created_at < '2026-11-01' ) AND NOT EXISTS ( SELECT 1 FROM orders o WHERE o.user_id = u.id AND o.created_at >= '2026-11-01' );PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative October-only purchasers2 sample rows
| id | |
|---|---|
| 23 | asha@example.com |
| 68 | kabir@example.com |
Note: Illustrative users with an October order and no November order. The upper date bound in the NOT EXISTS intentionally matches the example.
Key takeaway: Set operations for list comparisons; EXISTS when you need other columns or key-based matching.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Build two comparable queries
Select exactly the columns that define "the same row", in the same order and types, on both sides.
SELECT user_id … /SELECT user_id …When to use it & trade-offs
What it is
INTERSECT returns rows that appear in both queries' results; EXCEPT returns rows from the first query that don't appear in the second (Oracle calls it MINUS). Like UNION, they match columns by position and remove duplicates by default. Unlike joins, they compare whole rows and treat NULLs as equal.
Why it matters: They're the most direct way to compare two datasets: what's in both lists, what's missing from one, or whether two versions of a table differ at all. They're also a quick answer to "users who did X and Y" questions, and interviewers like to compare them with EXISTS and joins.
When to reach for it
- Verifying data migrations and backfills
- Users who did X and Y, or X but not Y
- Finding rows missing from one system
When to avoid or be careful
- Selecting extra columns that make otherwise matching rows look different
- Assuming EXCEPT keeps duplicates
- Using INTERSECT/EXCEPT on MySQL versions before 8.0.31
EXCEPT vs NOT EXISTS vs LEFT JOIN … IS NULL
Strengths: Short, NULL-safe, compares whole rows.
Watch out: Returns only the compared columns; deduplicates.
Strengths: Flexible matching; returns any columns of the outer table.
Watch out: More verbose.
Strengths: Works everywhere.
Watch out: Easy to break with extra WHERE conditions; NULL keys never match.
Interview signals & numbers to know
When this topic comes up
- “Users who bought in both months”
- “What's in table A but not table B?”
- “Did the migration copy everything?”
- Set operations
- MINUS in Oracle code
Benchmarks & rules of thumb
- 2
- directions needed for a full table diff
- NULL = NULL
- treated as equal by set operations
- MySQL 8.0.31
- INTERSECT and EXCEPT added
Common mistakes & follow-ups
Common interview mistakes
- Including a timestamp column in both queries, so no rows ever match.
- Writing B EXCEPT A when you meant A EXCEPT B.
- Checking only one direction of a table diff and missing rows added to the new table.
Likely interviewer follow-ups
- How would you check whether two tables contain exactly the same rows?
- When would you use NOT EXISTS instead of EXCEPT?
- How do INTERSECT and EXCEPT treat NULL values?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Set Differences & Intersections (INTERSECT, EXCEPT) with hands-on SQL practice and interview Q&A.