EXCEPT
Part of the Beyond the Basics section of Coddy's SQL journey — lesson 18 of 27.
EXCEPT returns rows that appear in the first result set but not in the second. Think of it as set subtraction.
SELECT email FROM newsletter_subs
EXCEPT
SELECT email FROM premium_usersEvery newsletter subscriber who isn't a premium user. This is useful for finding missing rows: who's signed up but hasn't paid? Which products were ordered last month but not this month?
Like UNION and INTERSECT, it removes duplicates and expects matching columns on both sides.
Challenge
EasyAvailable tables and columns:
<strong>last_month</strong>:<strong>product</strong><strong>this_month</strong>:<strong>product</strong>
Return one column product: every product that was sold last month but not this month. Order alphabetically.
Cheat sheet
EXCEPT returns rows from the first result set that do not appear in the second (set subtraction). Duplicates are removed automatically.
SELECT email FROM newsletter_subs
EXCEPT
SELECT email FROM premium_usersBoth sides must have the same number of columns with compatible types.
Try it yourself
-- products in last_month but not this_month
This lesson includes a short quiz. Start the lesson to answer it and track your progress.