QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › JOIN

What is the difference between a condition in ON and in WHERE with a LEFT JOIN?

A condition in ON decides what counts as a pair, so left rows without a pair are kept, while WHERE filters the joined result. An ordinary comparison with a right-table column is UNKNOWN on rows without a pair, so WHERE drops them and the LEFT JOIN becomes an INNER one. Hence conditions on the right table go into ON, and WHERE keeps left-table filters and the IS NULL of an anti-join.

SQL · JOIN

The report should show all users and their active accounts, and only Anna is left. What broke the LEFT JOIN?

The report "all users and active accounts, if any" suddenly shows one user. The query is written with a LEFT JOIN, so everyone should remain. The difference is where the condition status = 'active' is written: in ON or in WHERE. One moved line changes the meaning of the query, and there is no error. This lesson explains when each condition runs and why it separates LEFT JOIN from INNER.

How it works and why. The ON condition is part of the join itself: it decides which rows of the right table count as a pair for a left row. If none passed, the left table still keeps the row with NULL on the right. WHERE runs after the join, when a "row with no pair" already has NULL in its columns. The condition a.status = 'active' on such a row gives UNKNOWN (NULL equals nothing), and WHERE drops it. The result: all rows without a pair vanished, and LEFT JOIN became INNER JOIN. This order is part of the logical sequence of a query: first FROM … JOIN … ON, then WHERE.

Example: from one user to three. Users Anna, Ivan, Olha. Accounts: 10 (Anna, active), 11 (Anna, closed), 12 (Ivan, closed). Olha has no account.

-- wrong: WHERE after the join drops rows with NULL
SELECT u.email, a.id AS account_id
FROM users u
LEFT JOIN accounts a ON a.user_id = u.id
WHERE a.status = 'active';
-- 1 row: only Anna

-- right: the pair condition in ON
SELECT u.email, a.id AS account_id
FROM users u
LEFT JOIN accounts a
  ON a.user_id = u.id AND a.status = 'active';
-- 3 rows: Anna with 10, Ivan and Olha with NULL

WHERE versus ON on the same data

LEFT JOIN with no filter
emailaccountstatus
anna10active
anna11closed dropped
ivan12closed dropped
olhaNULLNULL dropped
condition in ON
emailaccountstatus
anna10active
ivanNULLNULL
olhaNULLNULL
Figure 1. WHERE keeps only Anna’s row. The condition in ON keeps all users, but does not attach the closed account to Ivan.

Let us take it apart. The first query returns only Anna with account 10: the rows of Ivan and Olha are dropped, because closed = 'active' is false, and for Olha it is UNKNOWN. The second query keeps everyone, and Ivan is shown with NULL: a closed account does not count as a pair. If that is the report needed ("a user and their active account, if there is one"), only the second is right. A sign that the query is spoiled: in a report on "everyone" the row count equals that of an INNER JOIN.

Limits and alternatives. The rule "a condition on the right table goes in ON" applies where the left rows must be kept. There are three situations where WHERE on the right table is justified. First: INNER JOIN, where there is no difference between ON and WHERE. Second: the orphan search, where WHERE a.id IS NULL deliberately tests for NULL. Third: a filter that really must remove users without a matching account, that is an INNER JOIN is needed, and it is better written as one.

There is a reverse case that surprises people. A condition on the left table in ON does not remove left rows, because LEFT JOIN keeps them by definition:

-- a condition on the LEFT table in ON does not remove its rows
SELECT u.email, o.id
FROM users u
LEFT JOIN orders o
  ON o.user_id = u.id AND u.email = 'anna@test.com';
-- 4 rows: Anna 100, Anna 101, Ivan NULL, Olha NULL

-- to filter the left side you need WHERE
SELECT u.email, o.id
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.email = 'anna@test.com';

In the first query Ivan and Olha stay, and Ivan is shown with no order: the condition u.email = … only denied him a pair. Whoever writes such a condition usually wanted to filter users, and the result turns out wrong with no signal. For the left table the filter is always in WHERE. For FULL JOIN both sides are kept, so a condition in ON removes rows from neither, and a condition in WHERE on one side's column drops the rows that exist only in the other table (that column is NULL in them), so the FULL JOIN becomes a LEFT or RIGHT JOIN (conditions on both sides make it INNER).

Where to put the condition

Typical

WHERE a.status = 'active'

Filters the finished result. Rows without a pair disappear, LEFT JOIN becomes INNER.

Better

ON … AND a.status = 'active'

The condition is part of the pair search. All left rows are in place, NULL where there is no pair.

Figure 2. A condition on the right table in ON limits what is attached. In WHERE it filters out the result.

Common mistakes.

  • A condition on the right table of a LEFT JOIN in WHERE: rows without a pair vanish, LEFT JOIN becomes INNER.
  • A condition on the left table in ON in the hope of filtering it: the left side is not filtered.
  • Expecting "all users" from a report when the query has already turned into INNER: fewer rows than in the table and no error.
  • "Treating" the vanished rows by adding OR a.id IS NULL to WHERE: it works by accident and breaks when the data changes.
  • Not checking the row count of a LEFT JOIN: it is not smaller than the left table, and a smaller number means a wrong WHERE.

Check yourself

users has 100 rows. The query users u LEFT JOIN accounts a ON a.user_id = u.id WHERE a.status = 'active' returned 40 rows. Does it mean 60 users have no active account?

Answer

Not necessarily: 40 is rows, not users. If a user has two active accounts, they give two rows, so fewer than 40 users may have an active account and more than 60 have none. For a report on all 100, move the condition into ON: then there will be at least 100 rows.

Why does LEFT JOIN orders o ON o.user_id = u.id AND u.country = 'UA' return users from all countries?

Answer

A condition on the left table in ON only denies users from other countries a pair, and LEFT JOIN keeps them with NULL in the right columns. A country filter must be in WHERE.

In an interview. Junior: With LEFT JOIN a condition on the right table in ON only limits what is attached, and all left rows stay. The same condition in WHERE drops rows with NULL on the right, and LEFT JOIN effectively becomes INNER. Middle/Senior: ON works during the join, WHERE after it, and NULL gives UNKNOWN. So I write the pair condition for the right table in ON and a filter on the left in WHERE. The exception is an anti-join, where WHERE a.id IS NULL is deliberate. A query like this runs without errors and returns less data, so I check that the row count is not smaller than the left table.

More articles in this chapter