Stop 07 · Subqueries, CTEs & Set OperationsTopics · 9/9

INTERSECT and EXCEPT

Part of the SQL Roadmap

In short

"Who bought in both September and October?" and "Who bought in September but not October?" are retention questions. INTERSECT and EXCEPT answer them by comparing two whole lists.

INTERSECT returns rows found in both queries and EXCEPT returns rows in the first but not the second. Oracle calls EXCEPT MINUS, and MySQL added both in 8.0.31.

The jaadu holds two sign-in boards, one decorated with garba dandiya sticks and one with glowing diyas, pulling out the faces that appear on both while a few faces stay on only one.

Think of it like this

Garba night and the Diwali mela

A housing society in Ahmedabad keeps two sign-in sheets: one from Navratri garba night and one from the Diwali mela. People on both sheets are the regulars: that's INTERSECT. People who came to garba but skipped the mela are the ones to call: that's EXCEPT. Same two sheets, two very different questions.

Watch it happen

Two set operations on September and October buyers

INTERSECT

  • Rows found in both queries
  • Here: customers who bought in both months
  • Order of the two queries doesn't matter
  • Removes duplicates, like UNION

EXCEPT

  • Rows in the first query but not the second
  • Here: September buyers who didn't return in October
  • Order matters: A EXCEPT B ≠ B EXCEPT A
  • Oracle calls it MINUS

INTERSECT finds overlap, EXCEPT finds what's missing. Select only the key columns so rows can actually match.

The big ideas

What you need to know

Whole rows are compared

A row matches only if every selected column matches. Select just customer_id, not dates or amounts.

EXCEPT has a direction

September EXCEPT October gives lapsed customers. October EXCEPT September gives new or returning ones.

NULLs match here

Unlike =, set operations treat two NULLs as equal when comparing rows.

Support varies

PostgreSQL and SQL Server have both. MySQL added them in 8.0.31. Oracle uses MINUS for EXCEPT.

See it in code

Walk through the code

sql
1-- Bought in September but NOT in October (lapsed)2SELECT customer_id3FROM orders4WHERE order_date >= DATE '2026-09-01'5  AND order_date <  DATE '2026-10-01'6 7EXCEPT8 9SELECT customer_id10FROM orders11WHERE order_date >= DATE '2026-10-01'12  AND order_date <  DATE '2026-11-01';13 14-- Swap EXCEPT for INTERSECT to get customers who bought in both months.

1/4First list: everyone who ordered in September.

Customers who lapsed after September. Works in PostgreSQL, SQL Server and MySQL 8.0.31 or later; use MINUS in Oracle.

Save yourself the pain

Common mistakes

Oops

SELECT customer_id, order_date in both halves.

Do this instead

Dates differ between months, so no row ever matches. Select only the columns that define 'the same customer'.

Oops

Writing October EXCEPT September for lapsed customers.

Do this instead

That gives people who bought in October but not September. Put the 'from' list first.

Oops

Expecting EXCEPT to keep duplicate customer IDs.

Do this instead

EXCEPT and INTERSECT remove duplicates. PostgreSQL offers EXCEPT ALL and INTERSECT ALL if you need them kept.

Your mission

Retention in two queries

~20 min

Quick check

List A has customers 1, 2, 3, 4 and list B has 3, 4, 5. What does A EXCEPT B return?

Go deeper

  1. 1Find customers who bought in both January and February with INTERSECT.
  2. 2Find customers who bought in January but not February with EXCEPT.
  3. 3Rewrite one of them with joins to understand the logic.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge