Results.

SCEPTRA.TECH

SQLite LEFT JOIN Loses Accounts? Put Match Filters in ON When Every Account Must Stay

LEFT JOIN match filtering keeps all accounts while WHERE and OR NULL variants give different results

A report should list every account and attach any open support tickets. Yet accounts with no open ticket disappear after a LEFT JOIN. Before changing the data or adding DISTINCT, I would check whether the ticket filter belongs in ON or WHERE. Those clauses answer different questions in an outer join.

Define the report before fixing the query

Use a small example: North has one open ticket, West has one closed ticket, and East has no tickets. The report needs all three accounts. An account without an open ticket should appear with NULL ticket columns.

This query does not meet that requirement:

SELECT a.name, t.id, t.state
FROM accounts AS a
LEFT JOIN tickets AS t ON t.account_id = a.id
WHERE t.state = 'open'
ORDER BY a.id;

North appears. West has a matched ticket, but its closed state fails the filter. East gets a NULL-extended row from the left join, but NULL = 'open' is not true, so WHERE removes that row too.

Filter the matches, not every output row

If the requirement is every account plus its open tickets, put the ticket qualification alongside the relationship condition:

SELECT a.name, t.id, t.state
FROM accounts AS a
LEFT JOIN tickets AS t
  ON t.account_id = a.id
 AND t.state = 'open'
ORDER BY a.id, t.id;

Now only open tickets qualify as matches. When an account has none, LEFT JOIN preserves that account with NULL values on the ticket side. The expected result is North with its open ticket, West with NULLs, and East with NULLs.

SQLite's SELECT documentation describes the important boundary: unmatched NULL-extended rows are added after ON processing and before WHERE processing. WHERE then keeps only expressions that evaluate to true. This is a model for understanding the result, not a claim about the physical order of every operation inside the optimizer.

Why adding OR IS NULL is not equivalent

A tempting patch leaves the first join unchanged and uses WHERE t.state = 'open' OR t.id IS NULL. It brings East back, but not West. West already matched a closed ticket during the join. There is no additional unmatched row for that account, and its real ticket ID is not NULL.

In the tiny schema below, id is an INTEGER PRIMARY KEY, so the test uses a non-NULL identifier for real tickets. Even with that clear identifier, the OR patch answers a different question from filtering the matches in ON.

CREATE TABLE accounts (
  id INTEGER PRIMARY KEY, name TEXT
);
CREATE TABLE tickets (
  id INTEGER PRIMARY KEY, account_id INTEGER, state TEXT
);

Keep intentional output filters

Do not move every WHERE condition into ON. If the report should contain only accounts with open tickets, excluding unmatched accounts is intentional, and an INNER JOIN may express that intent more directly. Conditions that select which accounts belong in the report can still belong in WHERE.

Also, LEFT JOIN does not guarantee one row per account. Two qualifying tickets produce two rows for that account. If the report needs one account row, decide whether it needs aggregation, an existence check or a deliberately selected ticket. DISTINCT is not a substitute for that decision.

What I checked locally

I ran the three query variants against an in-memory SQLite database using Python. The WHERE-only query returned North. The ON-filtered query returned all three accounts. The OR-NULL patch returned North and East, missing West. Adding a second open ticket to North produced two North rows with the ON-filtered query.

These are correctness checks on a tiny example, not a production performance test. Reproduce the cases against a safe sample before changing a live report. The useful question is which left-side rows must survive, and which right-side rows count as matches.

Sources

Comments