Overview
Find the customers who never ordered with NOT EXISTS, and see why NOT IN returns nothing as soon as one NULL shows up.
- NOT EXISTS (subquery)
- Correlated subqueries
- The NOT IN + NULL trap
Short Course
To find rows with no match, like customers who never ordered, use
NOT EXISTS. Trap: NOT IN (subquery) returns no rows at all if the
subquery contains a single NULL, such as a guest order with no customer.
SELECT p.name
FROM products AS p
WHERE NOT EXISTS (
SELECT 1 FROM order_items AS i WHERE i.product_id = p.id
); -- products never sold
Task
Return the name of every customer who has never placed an order, ordered by
name. In the database below, one order is a guest checkout with no customer.
Database
Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.
| idINTEGER | nameTEXT |
|---|---|
| 1 | Ana |
| 2 | Bo |
| 3 | Cy |
| 4 | Di |
| idINTEGER | customer_idINTEGER |
|---|---|
| 10 | 1 |
| 11 | 3 |
| 12 | NULL |
Test Suites
Pending
Next
Output