Brightlane's CRM team is running a reactivation campaign and needs to identify customers who have not yet placed an order.
Write a query to return the names of every customer with no order history.
Assumptions:
- A customer with zero orders is one whose
customer_iddoes not appear anywhere in theorderstable.
Output:
- One row per customer with no order history, with a single column
name.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
c.name
FROM
customers c
LEFT JOIN orders o ON c.id = o.customer_id
WHERE
o.id IS NULL The shape
The LEFT JOIN keeps every customer; the WHERE o.id IS NULL filter then keeps only the customers whose right-side columns came back as NULL — meaning the join found no matching order. The pair is the anti-join: surface every entity on the left that has no counterpart on the right.
Clause by clause
SELECT c.namereturns just the customer name — the only column the reactivation campaign needs.FROM customers c LEFT JOIN orders o ON c.id = o.customer_idpairs each customer with each of their orders. Customers with at least one order produce one row per order with real values ino.*. Customers with no orders produce a single row withNULLin everyo.*column.WHERE o.id IS NULLkeeps only those single-row, unmatched customers.orders.idis the primary key of theorderstable — a real order can never have aNULLid. So aNULLino.idis unambiguous: the row was synthesised by theLEFT JOINto preserve an unmatched left-side customer. The eight customers in the result — Omar Jensen, Mark Hayes, and so on — are exactly the eight who don't appear anywhere inorders.customer_id.
Why this and not WHERE o.customer_id IS NULL
Both checks work on this query because the right side of the join produces NULL in every right-table column simultaneously when there's no match. The convention is to check a primary key column (o.id) rather than the join key (o.customer_id), because the primary key is guaranteed non-nullable on real rows — there is no ambiguity between "the row is real and has NULL in this column" and "the row was synthesised by the outer join."
The trap
The trap is putting the existence check on the left-side key by accident. WHERE c.id IS NULL filters out everyone — customers.id is the primary key on the left table and is never NULL on a real row. The query runs, returns zero rows, and looks like a perfectly empty result set rather than a logic error. The IS NULL check belongs on the right side of a LEFT JOIN. The right-side column is the one that becomes NULL when no match exists; the left-side column always carries its real value.
You practiced the anti-join pattern: LEFT JOIN ... WHERE right_table.key IS NULL. The recurring shape any time the question is "which entities on the left have no counterpart on the right" — the LEFT JOIN preserves everyone, the IS NULL check on a right-side key keeps only the unmatched ones.