Keeping unmatched rows

Lesson 16 of 18 · SQL foundations

A left join preserves every row from the left input. When no right row matches, SQLite fills the right-side columns with NULL. The left input can still appear multiple times if it has multiple matches.

SELECT shelves.label, COUNT(books.id) AS book_count
FROM shelves LEFT JOIN books ON books.shelf_id = shelves.id
GROUP BY shelves.id, shelves.label ORDER BY shelves.id;

The result is Nature / 2, History / 1, Art / 0. COUNT(books.id) ignores the NULL placeholder for Art; COUNT(*) would count that placeholder row and yield 1. To keep all shelves while counting only published books, put books.published = 1 in the ON condition. Putting it in WHERE removes unmatched rows because NULL is not true.

left row + matching right rows → one output per match
left row + no match           → one output with right columns NULL

A right join preserves the right side, and a full outer join preserves unmatched rows on both sides. SQLite supports RIGHT and FULL starting with 3.39; older versions require alternatives. Other dialect support differs. SQL joins match row pairs, not simply overlapping sets.

Compare RIGHT and FULL with the same inputs

Use a fresh copy of the setup fixture. books is the left input and shelves the right. The fixture has two Nature books, one History book, Night Sky with no shelf, and Art with no books. These queries require SQLite 3.39.0 or later for RIGHT and FULL. Check the actual client with SELECT sqlite_version();; a separate Python installation may link a different SQLite version.

SELECT books.title, shelves.label
FROM books RIGHT JOIN shelves ON books.shelf_id = shelves.id
ORDER BY shelves.id, books.id;
title label
Moss Atlas Nature
River Notes Nature
Clay Roads History
NULL Art

RIGHT preserves Art, but not Night Sky: the latter has no matching right-side shelf. Swapping the input tables and using LEFT with the same ON condition gives the same pairs, though column order and ordering need attention.

SELECT books.title, shelves.label
FROM books FULL OUTER JOIN shelves ON books.shelf_id = shelves.id
ORDER BY COALESCE(books.id, 999), shelves.id;
title label
Moss Atlas Nature
River Notes Nature
Clay Roads History
Night Sky NULL
NULL Art

FULL keeps both unmatched rows as well as each matching pair. The 999 sorting value works for this small fixture; it is not a general sentinel for arbitrary IDs. NULL in the output may also be an actual input value, so test a guaranteed non-NULL key when identifying an unmatched row.

For older SQLite versions, RIGHT can be rewritten as LEFT with the inputs swapped. For FULL, combine a left join with only the unmatched right rows from a reversed left join. UNION ALL retains distinct matching pairs and does not silently remove duplicate-looking results:

SELECT books.title, shelves.label
FROM books LEFT JOIN shelves ON books.shelf_id = shelves.id
UNION ALL
SELECT books.title, shelves.label
FROM shelves LEFT JOIN books ON books.shelf_id = shelves.id
WHERE books.id IS NULL;

This returns the same five pairs as the FULL example, but without an ORDER BY their order is not guaranteed. The books.id primary key cannot be NULL in a real matching row; that makes the unmatched test valid here. For other schemas, choose a guaranteed non-NULL column.

Chapter index · Course index