Cheatsheet Pages / SQL Cheatsheet
How to find rows with no match in SQL — LEFT JOIN ... IS NULL
You are hunting rows that should have related records but do not — employees who never placed an order.
Every example runs against the cheatsheet's sample tables — departments(id, name), employees(id, name, dept_id, salary, manager_id, hired_on) and orders(id, emp_id, amount, ordered_on) — and was executed and verified against SQLite, so you can paste it in and get the same result.
SELECT e.name FROM employees e LEFT JOIN orders o ON o.emp_id = e.id WHERE o.id IS NULL;
-- Carol / Frank / Grace: employees who never ordered. The classic orphan-finder
Good to know
The classic orphan-finder: keep every left-table row, then keep only the ones where the joined table's key came back NULL.
