Lesson 24 — EXISTS vs IN
The NOT IN trap with NULL
IN (subquery) and EXISTS usually give the same answer. Their negations do not. If the subquery list contains a NULL, x NOT IN (...) is never true: SQL cannot be sure x is not the unknown value, so it returns no rows at all. NOT EXISTS only checks real matches, so NULLs do not break it.
SELECT 1 WHERE 3 NOT IN (1, NULL) -- returns nothing!
Why it matters: this bug makes "customers who never ordered" reports silently empty the day one order has a missing customer_id. Prefer NOT EXISTS.
Rewrite the second query with NOT EXISTS. Expected: NOT IN finds [], NOT EXISTS finds ['Bo'].
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based SQL Deep Dive courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for SQL Deep Dive books — check the publication year so the examples match current versions.
Search Amazon →