Databases & SQL Master Course — 033. Outer Join Filter Placement
Lesson 033 Outer Join Filter Placement
Two LEFT JOIN queries can use the same tables and the same stock condition yet answer different questions. A condition in ON controls which books count as matches. A condition in WHERE controls which joined rows survive. This distinction determines whether authors without qualifying books remain visible.
Prerequisites and objectives
Complete Lesson 032. You should understand preserved rows, NULL extension, COUNT of a matched key, and Boolean conditions. You will choose filter placement from a reporting requirement, trace unmatched and nonqualifying cases, and explain why adding OR key IS NULL is not a universal repair.
Step 1 Build the contrast cases
Run this setup once in an empty SQLite scratch database. Mira and Omar have in-stock books. Nia has a book, but its stock is zero. Elio has no book. Those last two authors must remain distinct while we reason about qualifying matches.
PRAGMA foreign_keys = ON;
CREATE TABLE authors (
author_id INTEGER PRIMARY KEY,
author_name TEXT NOT NULL
);
CREATE TABLE books (
book_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
author_id INTEGER NOT NULL REFERENCES authors(author_id),
genre TEXT NOT NULL,
price_cents INTEGER NOT NULL,
stock INTEGER NOT NULL,
subtitle TEXT
);
INSERT INTO authors VALUES
(1,'Mira Chen'),(2,'Omar Silva'),(3,'Nia Patel'),(4,'Elio Park');
INSERT INTO books VALUES
(101,'River Maps',1,'Travel',1800,4,NULL),
(102,'SQL Trail',2,'Technology',2500,8,'Practice edition'),
(103,'Garden Notes',3,'Nature',1200,0,NULL),
(104,'Night Signals',1,'Fiction',1800,5,'');
The report request is “show every author and any available books.” Available means known stock greater than zero. It does not mean that authors without available books should disappear. Write that preservation requirement down before deciding where to put the stock predicate.
Step 2 Put match eligibility in ON
The author relationship and the positive-stock requirement together define a qualifying book match.
SELECT a.author_id, a.author_name, b.book_id, b.stock
FROM authors AS a
LEFT JOIN books AS b
ON b.author_id = a.author_id AND b.stock > 0
ORDER BY a.author_id, b.book_id;
Nia and Elio both receive a preserved row with NULL book fields. Nia’s zero-stock book did not satisfy the complete ON condition, so it was not a qualifying match. Elio had no relationship match at all. The result answers the available-books question without claiming those underlying situations are identical.
Step 3 Compare a WHERE filter
Move the same stock condition into WHERE while leaving ON as the author relationship only.
SELECT a.author_id, a.author_name, b.book_id, b.stock
FROM authors AS a LEFT JOIN books AS b ON b.author_id = a.author_id
WHERE b.stock > 0
ORDER BY a.author_id, b.book_id;
Nia first matches her zero-stock book, then that row fails WHERE. Elio gets NULL book fields, and NULL > 0 is unknown, so his row also fails WHERE. Only authors with qualifying matched book rows remain. That can be a valid report, but it is not the stated every-author report.
This explanation describes logical query meaning, not a mandated physical execution sequence. The optimizer may choose a different implementation while preserving those semantics. Do not use left-to-right predicate order as a safety mechanism for arbitrary expressions.
Step 4 Test a tempting repair
A learner adds OR b.book_id IS NULL to the WHERE condition, hoping to recover all authors. Examine its result.
SELECT a.author_id, b.book_id
FROM authors AS a LEFT JOIN books AS b ON b.author_id = a.author_id
WHERE b.stock > 0 OR b.book_id IS NULL
ORDER BY a.author_id, b.book_id;
Elio returns because his preserved row has a NULL book key. Nia still disappears: her real zero-stock book had a non-NULL key, so neither branch passes. A WHERE condition does not manufacture a new unmatched row after rejecting an existing match. This counterexample is why the shortcut is not equivalent to filtering matches in ON.

Step 5 Count qualifying matches
If the request is one row per author with the number of available titles, keep the stock condition in ON and count the matched key.
SELECT a.author_id, COUNT(b.book_id) AS available_titles
FROM authors AS a LEFT JOIN books AS b
ON b.author_id = a.author_id AND b.stock > 0
GROUP BY a.author_id ORDER BY a.author_id;
The counts are 2, 1, 0, and 0. Here zero correctly means no book matches the available-title definition. It does not assert that the author has no books of any kind. The alias must preserve that scope; calling it total_books would mislead a reader about Nia.
Step 6 Filter the preserved population separately
A condition on the author table can intentionally narrow which authors are in scope. For example, WHERE a.author_id <= 3 removes Elio from the preserved population while still keeping Nia with zero qualifying books. Place relationship and right-side eligibility in ON, and place the intended left-population restriction in WHERE.
These are useful design habits rather than a rule that every predicate has exactly one valid location in every query. Choose placement by desired semantics, then verify with an author having several matches, one having only nonqualifying matches, and one having no matches.
Practice with worked solutions
Exercise 1
List every author with any Technology book, preserving authors without one.
Worked solution
SELECT a.author_id, b.book_id
FROM authors AS a LEFT JOIN books AS b
ON b.author_id = a.author_id AND b.genre = 'Technology'
ORDER BY a.author_id, b.book_id;
Only Omar has a qualifying Technology match. The other authors remain with NULL book IDs because the genre condition defines optional match eligibility.
Exercise 2
List authors with no available titles, using the positive-stock condition as part of the join.
Worked solution
SELECT a.author_id, a.author_name
FROM authors AS a LEFT JOIN books AS b
ON b.author_id = a.author_id AND b.stock > 0
WHERE b.book_id IS NULL
ORDER BY a.author_id;
Nia and Elio qualify. The result means no available-title match, not necessarily no catalog book. Nia is the counterexample proving why the wording matters.
Exercise 3
For author IDs at most 3, count available titles while retaining zero counts.
Worked solution
SELECT a.author_id, COUNT(b.book_id) AS available_titles
FROM authors AS a LEFT JOIN books AS b
ON b.author_id = a.author_id AND b.stock > 0
WHERE a.author_id <= 3
GROUP BY a.author_id ORDER BY a.author_id;
The author-side WHERE restriction chooses the population. The right-side ON condition chooses qualifying books. Nia remains with zero, while Elio is intentionally out of scope.
Exercise 4
Return only actual in-stock book matches, with no requirement to preserve every author.
Worked solution
SELECT a.author_id, b.book_id
FROM authors AS a INNER JOIN books AS b ON b.author_id = a.author_id
WHERE b.stock > 0
ORDER BY a.author_id, b.book_id;
An INNER JOIN with a stock filter states this narrower requirement directly. It returns three book matches. Preservation is unnecessary when the question specifically requires an actual available book.
Knowledge check
Question 1
Where should positive stock go to preserve every author with optional available books?
- Only in ORDER BY
- In the ON matching condition
- Only in LIMIT
- In WHERE with OR b.book_id IS NULL
Answer and explanation
B. It defines which right-side rows qualify as matches while preserving the left input.
Question 2
Why does a right-side WHERE stock > 0 remove Elio?
- It deletes him
- LEFT JOIN removes authors without relationship matches
- His generated NULL stock does not make the predicate true
- He has negative stock
Answer and explanation
C. WHERE excludes unknown conditions; the stored author is unchanged.
Question 3
Why does OR b.book_id IS NULL fail to recover Nia?
- Her matched zero-stock book has a non-NULL key
- Her author ID is NULL
- WHERE creates a new unmatched row for her
- The book table is empty
Answer and explanation
A. She had a real relationship match that the later filter rejects.
Question 4
What does available_titles equal zero establish?
- No books ever existed
- No author exists
- No subtitle exists
- No book satisfies the available-title match rule
Answer and explanation
D. The measure is scoped to the positive-stock condition, not all books.
Question 5
Which case is essential when testing outer-filter placement?
- Only many successful matches
- A parent with only nonqualifying children
- Only one table
- Only distinct names
Answer and explanation
B. Nia’s case exposes the difference between no qualifying match and a match later filtered away.
Pitfalls and summary
Do not move optional-side filters casually between ON and WHERE or assume an OR NULL branch repairs every loss. Define the preserved population and qualifying relationship separately. Test both absent children and existing children that fail eligibility; they are different cases with different query consequences.
SQLite behavior and references
Examples were checked with SQLite 3.53.1 on 10 October 2026. Use a SQLite SQL editor; Python is not a prerequisite. Verify dialect-specific functions, comparison rules, and numeric behavior before moving the examples to another engine.
Primary references
- SQLite SELECT joins and WHERE: Outer-join NULL extension and filter placement.
- SQLite expressions: Boolean and missing-value conditions.
- SQLite aggregate functions: Counting only matched keys.