Finding what's missing
A LEFT JOIN followed by WHERE … IS NULL on the right-hand table finds the rows with no
match. These are the customers who have never placed an order:
SELECT c.id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULLEvery customer is kept, those with orders get their orders attached, and the WHERE keeps only
the ones where nothing was attached.
It's one of the most useful patterns in business SQL: customers who haven't ordered, products that
haven't sold, payments that don't match an invoice. Check the right-hand table's key (o.id), which
is only NULL when there was no match.