# How to find rows that aren't in another table in SQL

Source: https://sqlstreak.com/sql/how-to/rows-not-in-other-table · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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.

```sql
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 (both engines):

| name | species |
| --- | --- |
| Bean | dog |
| Clover | rabbit |
| Gizmo | dog |
| Juniper | rabbit |
| Maple | dog |
| Noodle | dog |
| Olive | cat |
| Pumpkin | cat |

(8 of 12 rows)

### The same with LEFT JOIN … IS NULL

```sql
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 (both engines):

| name | species |
| --- | --- |
| Bean | dog |
| Clover | rabbit |
| Gizmo | dog |
| Juniper | rabbit |
| Maple | dog |
| Noodle | dog |
| Olive | cat |
| Pumpkin | cat |

(8 of 12 rows)

### Just the keys: EXCEPT

When you only need the values, the set operator says it directly.

```sql
SELECT LOWER(name) AS pet FROM pets
EXCEPT
SELECT LOWER(TRIM(pet_name)) FROM adoption_requests
ORDER BY pet;
```

Result (both engines):

| pet |
| --- |
| bean |
| clover |
| gizmo |
| juniper |
| maple |
| noodle |
| olive |
| pumpkin |

(8 of 12 rows)

## How it works

- `NOT EXISTS (SELECT 1 …)`: True when the subquery finds no row.
- `WHERE LOWER(TRIM(r.pet_name)) = LOWER(p.name)`: The correlation: requests for this pet, ignoring case and stray spaces.

## 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

Same in both engines.
