▸_sqlgym

99. The NOT IN NULL Trap

HardNULL HandlingNOT EXISTSNOT IN pitfallthree-valued logic

You want every customer who has never placed an order. The obvious NOT IN (SELECT customer_id FROM orders) looks right — but orders.customer_id contains a NULL. With a NULL in the list, x NOT IN (..., NULL) can never evaluate to TRUE (it is NULL/unknown for every x), so the query silently returns zero rows. The robust fix is a correlated NOT EXISTS, which uses row-level matching and is immune to the NULL.

Task: return id and name of every customer with no matching order, using a NULL-safe approach. Order by id.

Output columns, in order: id, name.

(Note: the inner orders.customer_id set deliberately contains a NULL — this is exactly the case where NOT IN breaks and NOT EXISTS is required.)

Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…

This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases 5test cases in all. A sloppy query that only fits the example won't pass.

SQL
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.