Learn SQL
-- setops.intersect_except

INTERSECT & EXCEPT in SQL

What two queries share — and what only one has

--concept

INTERSECT returns rows that appear in BOTH query results; EXCEPT (MINUS in Oracle) returns rows of the first that never appear in the second. Both are set operations over entire result sets, and both remove duplicates by default — INTERSECT ALL / EXCEPT ALL keep them. They turn “which of these two lists overlap?” into a single query instead of a spreadsheet vlookup.

--how it works
1

Two results, one shape

Like UNION, both SELECTs must agree on column count and compatible types — the rule for every set operation.

2

INTERSECT keeps the overlap

Only rows present in BOTH results survive — and the output is distinct, even if a row appeared many times on each side.

3

EXCEPT keeps the difference

Rows of the first result with no match in the second survive; EXCEPT ALL would keep first-side duplicates.

4

Symmetry note

A EXCEPT B ≠ B EXCEPT A — order matters. INTERSECT is symmetric and associative; chain with parentheses when mixing.

5

Dedup is built in

Both operators remove duplicates unless you write INTERSECT ALL / EXCEPT ALL — unlike JOINs, which multiply.

--visualization
Oracle spells EXCEPT as MINUS
A — employeesEngineeringSalesMarketingB — departmentsEngineeringSalesMarketing3SELECT department FROM employees INTERSECT SELECT dept_id FROM departments;press Run to evaluate
Stage 1 / 4
Query A (employees) → {Engineering, Sales, Marketing}. Query B (departments) → the same three. Both deduped first.
--example
query.sql
1SELECT department FROM employees
2INTERSECT
3SELECT dept_id FROM departments;
Result
department
Engineering
Marketing
Sales

Both queries return the same three department names, so INTERSECT hands back all three. EXCEPT with the same inputs would return nothing — every employee department has a matching departments row.

--variants

INTERSECT — shared values

Which departments exist on both sides?

query.sql
1SELECT department FROM employees
2INTERSECT
3SELECT dept_id FROM departments
4ORDER BY 1;
Result
department
Engineering
Marketing
Sales
1Both sides resolve to the same three department names.
2All three are common to both results, so all three survive — deduplicated.
3Add a Research department with no employees and it disappears here — no employees reference it.

EXCEPT — first without second

Departments that have no employees (empty teams).

query.sql
1SELECT dept_id FROM departments
2EXCEPT
3SELECT department FROM employees;
Result
dept_id
Research
1Imagine Research was added to departments but nobody transferred yet.
2It appears on the left, never on the right — so it survives EXCEPT.
3This “left anti-join” reading — A without B — is the operator’s everyday use.

EXCEPT is not symmetric

Swapping the sides changes the answer.

query.sql
1SELECT department FROM employees
2EXCEPT
3SELECT dept_id FROM departments;
4-- vs
5SELECT dept_id FROM departments
6EXCEPT
7SELECT department FROM employees;
Result
result
query 1 → (empty)
query 2 → Research
1Every employee department has a matching departments row, so query 1 finds nothing to keep.
2Reversed, departments minus employee-departments leaves Research.
3Read A EXCEPT B aloud as “A but not B” — direction is the semantics.

EXCEPT ALL keeps multiplicities

Duplicate handling differs from the default.

query.sql
1SELECT department FROM employees WHERE department = 'Engineering'
2EXCEPT ALL
3SELECT dept_id FROM departments WHERE dept_id = 'Engineering';
4-- 3 Engineering rows minus 1 → 2 rows survive
Result
department
Engineering
Engineering
1EXCEPT (default) would collapse everything to nothing here — one distinct row on each side cancels out.
2EXCEPT ALL treats rows as a multiset: 3 − 1 = 2 survive.
3Think of ALL variants as arithmetic on counts rather than set algebra.
--key facts
Venn intuitionINTERSECT = middle overlap; EXCEPT = left circle minus overlap
DISTINCT built inboth deduplicate unless you write INTERSECT ALL / EXCEPT ALL
Dialect noteOracle spells EXCEPT as MINUS; MySQL historically lacks both — emulate with JOINs
--use cases
Reconciliation: rows in the ledger but not in the bank feed (EXCEPT), and vice versa
Feature flags: users who saw A INTERSECT users who saw B (co-occurrence)
Orphan checks: child keys EXCEPT parent keys must always be empty
Diffing two environments’ reference data before a migration
--under the hood
Both operations plan as semi/anti joins — hash-based for big inputs, merge-based when pre-sorted.
Oracle spells EXCEPT as MINUS; SQLite and Postgres use EXCEPT; MySQL offers neither — emulate with LEFT JOIN … WHERE b.key IS NULL.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.