INTERSECT and EXCEPT

Updated · 6 min read
mediumPostgreSQL 18sqlintersectexceptminusset-operations

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 it MINUS.
  • The column rules match UNION: same number of columns, compatible types.
  • Duplicates are removed unless you write INTERSECT ALL or EXCEPT ALL.
  • Unlike =, set operations treat two NULLs as the same value.
INTERSECT keeps what is in both; EXCEPT keeps what is only in the first

INTERSECT

SQL
SELECT columns FROM table_a
INTERSECT
SELECT columns FROM table_b;

Students who appear in both enrollments and payments:

SQL▶ Try it
SELECT student_id FROM enrollments
INTERSECT
SELECT student_id FROM payments
ORDER BY student_id;
Output
 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.

enrollments has 101, 102, 103; payments has 101, 103; the intersection is 101 and 103

EXCEPT

SQL
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:

SQL▶ Try it
SELECT student_id FROM enrollments
EXCEPT
SELECT student_id FROM payments
ORDER BY student_id;
Output
 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:

SQL▶ Try it
SELECT student_id, course_id FROM enrollments
EXCEPT
SELECT student_id, course_id FROM payments
ORDER BY student_id, course_id;
Output
 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.

Seven enrollment pairs minus the five paid pairs leaves Advitha's two unpaid enrollments

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:

SQL▶ Try it
SELECT method FROM payments WHERE student_id = 101
EXCEPT
SELECT method FROM payments WHERE student_id = 103
ORDER BY method;
Output
 method
--------
 NULL
(1 row)

The only method Srihitha used that Navika didn't is "no method recorded", shown as NULL.

Set operations treat NULL as equal to NULL. Srihitha's payment methods: card, check, NULL. Navika's: card, check. Srihitha's EXCEPT Navika's: NULL. card and check cancel out; NULL is only in Srihitha's list, so it remains. If both lists had NULL, they would match and cancel too. 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):

SQL▶ Try it
SELECT student_id FROM enrollments
INTERSECT ALL
SELECT student_id FROM payments
ORDER BY student_id;
Output
 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
SQL▶ Try it
SELECT course_id FROM courses
EXCEPT
SELECT course_id FROM enrollments;
Output
 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.

Quick check: INTERSECT and EXCEPT

3 questions
1enrollments has student_ids 101, 102, 103; payments has 101, 103. What does enrollments EXCEPT payments return?
2Srihitha's methods are card, check and NULL; Navika's are card and check. What is Srihitha's EXCEPT Navika's?
3What does Oracle call EXCEPT?

Key Takeaways

You want toWrite
In both listsSELECT student_id FROM enrollments INTERSECT SELECT student_id FROM payments
In the first onlySELECT student_id FROM enrollments EXCEPT SELECT student_id FROM payments
Oracle... MINUS ...

Was this tutorial helpful?

Lesson 44 of 115