INTERSECT and EXCEPT
INTERSECT returns the rows that appear in both results, and EXCEPT returns the rows of the first result that don't appear in the second. Together with UNION, they're the set operations: they compare whole rows from two queries. "Which students have enrolled and paid?" is an intersection. "Which enrollments have no payment yet?" is a difference. This lesson covers both, with the same column rules as UNION, plus two details that surprise people: how they handle NULL, and what the ALL versions do.
In One Minute
query1 INTERSECT query2: rows in both results.query1 EXCEPT query2: rows in the first result but not the second. Oracle calls itMINUS.- The column rules match
UNION: same number of columns, compatible types. - Duplicates are removed unless you write
INTERSECT ALLorEXCEPT ALL. - Unlike
=, set operations treat twoNULLs as the same value.
INTERSECT
SELECT columns FROM table_a
INTERSECT
SELECT columns FROM table_b;Students who appear in both enrollments and payments:
SELECT student_id FROM enrollments
INTERSECT
SELECT student_id FROM payments
ORDER BY student_id; student_id
------------
101
103
(2 rows)Srihitha (101) and Navika (103) have enrolled and paid. Advitha (102) has enrolled but appears in only one of the two results, so she isn't in the intersection.
EXCEPT
SELECT columns FROM table_a
EXCEPT
SELECT columns FROM table_b;EXCEPT subtracts the second result from the first. Students who enrolled but never paid:
SELECT student_id FROM enrollments
EXCEPT
SELECT student_id FROM payments
ORDER BY student_id; student_id
------------
102
(1 row)The order matters: payments EXCEPT enrollments would ask for students who paid without enrolling, and the foreign keys guarantee there are none. Comparing two columns finds enrollments with no payment at all:
SELECT student_id, course_id FROM enrollments
EXCEPT
SELECT student_id, course_id FROM payments
ORDER BY student_id, course_id; student_id | course_id
------------+-----------
102 | 1
102 | 4
(2 rows)A row is removed only if the whole row, both columns together, appears in the second result. Advitha's two enrollments have no payments. In Oracle, EXCEPT is spelled MINUS (Oracle 21c and later accept EXCEPT too). MySQL added INTERSECT and EXCEPT in version 8.0.31; older MySQL versions need a join or NOT EXISTS instead.
NULL Counts as Equal Here
Set operations treat two NULLs as the same value, unlike = in WHERE, where NULL = NULL is unknown. Srihitha has paid by card, by check, and once with no recorded method; Navika by card and check:
SELECT method FROM payments WHERE student_id = 101
EXCEPT
SELECT method FROM payments WHERE student_id = 103
ORDER BY method; method
--------
NULL
(1 row)The only method Srihitha used that Navika didn't is "no method recorded", shown as NULL.
UNION, INTERSECT and EXCEPT all compare rows this way, the same way DISTINCT does: if Navika also had a payment with no method, the two NULLs would match and this result would be empty.
The ALL Versions
INTERSECT ALL and EXCEPT ALL keep duplicates, counting how many times each row appears on each side. student_id appears four times in enrollments for Srihitha and five times in payments, so the intersection keeps four copies (the smaller count):
SELECT student_id FROM enrollments
INTERSECT ALL
SELECT student_id FROM payments
ORDER BY student_id; student_id
------------
101
101
101
101
103
(5 rows)Four 101s and one 103. You'll rarely need the ALL versions, but they're useful when counts matter, such as matching line items between two lists. SQL Server and Oracle support only the plain versions.
Common Mistakes
Swapping the order of EXCEPT. a EXCEPT b and b EXCEPT a answer different questions.
Comparing different columns. Columns line up by position: SELECT student_id, course_id ... EXCEPT SELECT course_id, student_id compares the wrong values.
Expecting NULL to behave as in WHERE. Set operations match NULL with NULL.
Needing other columns. EXCEPT returns only the compared columns. To show names too, use NOT EXISTS, covered in EXISTS.
Interview Corner
What's the difference between INTERSECT and EXCEPT?
INTERSECT returns rows present in both results; EXCEPT returns rows in the first result that aren't in the second.
What is MINUS?
Oracle's name for EXCEPT. Oracle 21c and later also accept EXCEPT.
How do set operations treat NULL?
They treat NULL values as equal to each other when comparing rows, as DISTINCT does, unlike the = operator.
Practice
Challenge: List the course ids that exist in courses but have no enrollments, using a set operation.
Show solution
SELECT course_id FROM courses
EXCEPT
SELECT course_id FROM enrollments; course_id
-----------
6
(1 row)Course 6, Git Basics, has nobody enrolled. To show its title too, you'd use NOT EXISTS or a left anti-join, which can return any columns.
Key Takeaways
| You want to | Write |
|---|---|
| In both lists | SELECT student_id FROM enrollments INTERSECT SELECT student_id FROM payments |
| In the first only | SELECT student_id FROM enrollments EXCEPT SELECT student_id FROM payments |
| Oracle | ... MINUS ... |
Was this tutorial helpful?