SQL how-to
How to find rows that aren't in another table in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
Use NOT EXISTS (SELECT 1 FROM other WHERE other.key = t.key), or LEFT JOIN other … WHERE other.key IS NULL. Both are correct even when the other table has NULLs, which NOT IN is not.
The task: Find the rows in one table that have no matching row in another, such as pets nobody has asked about.
Pets nobody has asked about
The adoption requests were typed by hand, so match on the cleaned name.
SELECT p.name, p.species
FROM pets p
WHERE NOT EXISTS (
SELECT 1 FROM adoption_requests r
WHERE LOWER(TRIM(r.pet_name)) = LOWER(p.name))
ORDER BY p.name;Result (PostgreSQL and SQLite, identical): 12 rows
| name | species |
|---|---|
| Bean | dog |
| Clover | rabbit |
| Gizmo | dog |
| Juniper | rabbit |
| Maple | dog |
| Noodle | dog |
| Olive | cat |
| Pumpkin | cat |
| Rocket | dog |
| Socks | cat |
| Waffles | rabbit |
| Ziggy | bird |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
NOT EXISTS (SELECT 1 …)True when the subquery finds no row.
- 2
WHERE LOWER(TRIM(r.pet_name)) = LOWER(p.name)The correlation: requests for this pet, ignoring case and stray spaces.
More examples
The same with LEFT JOIN … IS NULL
SELECT p.name, p.species
FROM pets p
LEFT JOIN adoption_requests r
ON LOWER(TRIM(r.pet_name)) = LOWER(p.name)
WHERE r.id IS NULL
ORDER BY p.name;Result (PostgreSQL and SQLite, identical): 12 rows
| name | species |
|---|---|
| Bean | dog |
| Clover | rabbit |
| Gizmo | dog |
| Juniper | rabbit |
| Maple | dog |
| Noodle | dog |
| Olive | cat |
| Pumpkin | cat |
| Rocket | dog |
| Socks | cat |
| Waffles | rabbit |
| Ziggy | bird |
Try it: PostgreSQL playground · SQLite playground
Just the keys: EXCEPT
When you only need the values, the set operator says it directly.
SELECT LOWER(name) AS pet FROM pets
EXCEPT
SELECT LOWER(TRIM(pet_name)) FROM adoption_requests
ORDER BY pet;Result (PostgreSQL and SQLite, identical): 12 rows
| pet |
|---|
| bean |
| clover |
| gizmo |
| juniper |
| maple |
| noodle |
| olive |
| pumpkin |
| rocket |
| socks |
| waffles |
| ziggy |
Try it: PostgreSQL playground · SQLite playground
Variations
Customers who never ordered
The classic version on clean keys: WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id).
Common mistakes
NOT IN (SELECT …)when the subquery can return NULL: the result is empty. See EXISTS vs IN.Filtering the right table's columns in WHERE after a LEFT JOIN, other than
IS NULL: it removes the unmatched rows you were looking for.Matching raw text with typos and different case: clean both sides first.
PostgreSQL and SQLite
Everything on this page works the same in PostgreSQL and SQLite: each example above was run on both, and the results match.
Official documentation
Go deeper
- Guide: SQL subqueries
- Practice: SQL subquery practice: 6 exercises with answers
- Lesson: LEFT JOIN (Pro)
Make it stick in five minutes a day
“LEFT JOIN” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.