# ON vs WHERE in a LEFT JOIN

Source: https://sqlstreak.com/sql/on-vs-where-in-left-join · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**Short answer:** In a `LEFT JOIN`, a condition in `ON` decides which right-hand rows match, and every left row stays. The same condition in `WHERE` runs after the join and removes the left rows that had no match, which silently turns the LEFT JOIN into an inner join.

## Side by side

| | Condition in ON | Condition in WHERE |
| --- | --- | --- |
| Runs | While matching rows | After the join |
| Left rows without a match | Kept, with NULLs | Removed |
| Result | Still a LEFT JOIN | Behaves like an INNER JOIN |
| Use it for | Filters on the right table | Filters on the left table |

## Examples

### Filter in ON: every customer stays

Each customer with their delivered orders; customers with none appear once with a NULL order.

```sql
SELECT c.name, o.id AS delivered_order
FROM customers c
LEFT JOIN orders o
  ON o.customer_id = c.id AND o.status = 'delivered'
ORDER BY c.name, o.id;
```

Result (both engines):

| name | delivered_order |
| --- | --- |
| Diego Santos | NULL |
| Ellie Park | 6 |
| Ellie Park | 14 |
| Jonah Reed | 2 |
| Jonah Reed | 7 |
| Jonah Reed | 13 |
| Lena Fischer | 10 |
| Marcus Bell | 12 |

(8 of 16 rows)

### Filter in WHERE: customers vanish

Same filter after the join: the NULL rows fail `o.status = 'delivered'`, so customers without a delivered order disappear.

```sql
SELECT c.name, o.id AS delivered_order
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'delivered'
ORDER BY c.name, o.id;
```

Result (both engines):

| name | delivered_order |
| --- | --- |
| Ellie Park | 6 |
| Ellie Park | 14 |
| Jonah Reed | 2 |
| Jonah Reed | 7 |
| Jonah Reed | 13 |
| Lena Fischer | 10 |
| Marcus Bell | 12 |
| Maya Ortiz | 1 |

(8 of 13 rows)

### The difference in numbers

```sql
SELECT
  (SELECT COUNT(DISTINCT c.id) FROM customers c
   LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'delivered') AS customers_with_on,
  (SELECT COUNT(DISTINCT c.id) FROM customers c
   LEFT JOIN orders o ON o.customer_id = c.id
   WHERE o.status = 'delivered') AS customers_with_where;
```

Result (both engines):

| customers_with_on | customers_with_where |
| --- | --- |
| 10 | 7 |

## When to use which

- **Condition in ON:** Conditions on the right-hand table when you want to keep every left row: “each customer and their delivered orders, if any”.
- **Condition in WHERE:** Conditions on the left-hand table (`WHERE c.city = 'Austin'`), or `WHERE o.id IS NULL` to find rows with no match.

## Common mistakes

- Filtering the right table in WHERE and wondering why the LEFT JOIN lost rows.
- Moving a left-table filter into ON: it doesn't remove left rows, it only stops them matching. Keep those in WHERE.
- Counting with `COUNT(*)` after the LEFT JOIN: unmatched rows count as 1. Count a right-hand column.
