INNER JOIN and LEFT JOIN, One Small Step at a Time
A JOIN combines rows from two tables that belong together. If you can already write a simple SELECT, this tutorial takes you through the two JOINs you will use most, using tables small enough to check by eye.
Step 1: make two tiny tables
CREATE TABLE authors (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE books (id INTEGER PRIMARY KEY, title TEXT, author_id INTEGER);
INSERT INTO authors VALUES (1, 'Ada'), (2, 'Ben'), (3, 'Cleo');
INSERT INTO books VALUES (10, 'Gardens', 1), (11, 'Rivers', 1), (12, 'Stars', 2), (13, 'Untitled', NULL);
Notice two things: Cleo has written no books, and the book "Untitled" has no author. Those two rows are what make JOINs interesting.
Step 2: an INNER JOIN
An INNER JOIN keeps only pairs that match on both sides.
SELECT authors.name, books.title
FROM authors
INNER JOIN books ON books.author_id = authors.id;
The ON part says how rows belong together: a book belongs to the author whose id equals the book's author_id.
| name | title |
|---|---|
| Ada | Gardens |
| Ada | Rivers |
| Ben | Stars |
Cleo disappeared because she has no matching book. "Untitled" disappeared because it has no matching author.
Step 3: a LEFT JOIN
A LEFT JOIN keeps every row from the left table (the one after FROM), matched or not.
SELECT authors.name, books.title
FROM authors
LEFT JOIN books ON books.author_id = authors.id;
| name | title |
|---|---|
| Ada | Gardens |
| Ada | Rivers |
| Ben | Stars |
| Cleo | NULL |
Cleo is back, with NULL (meaning "nothing here") where a title would be. "Untitled" is still missing, because it is on the right side and has no author.
Step 4: a worked question
"List every author and how many books they have, including authors with none."
SELECT authors.name, COUNT(books.id) AS book_count
FROM authors
LEFT JOIN books ON books.author_id = authors.id
GROUP BY authors.name;
Result: Ada 2, Ben 1, Cleo 0. We count books.id, not *, because COUNT(*) would count Cleo's empty row as 1.
Common mistakes
- Forgetting the ON condition. Without it, some databases pair every author with every book, which is rarely what you want.
- Filtering the right table in WHERE after a LEFT JOIN.
WHERE books.title = 'Stars'throws away Cleo's row, turning your LEFT JOIN back into an INNER JOIN. Put such conditions in the ON clause if you want to keep unmatched rows. Our session notes on LEFT JOINs that lose rows explain this one in detail. - Ambiguous column names. Both tables have
id. Writeauthors.idorbooks.idso the database knows which you mean. - Expecting one row per author. An author with three books appears three times. That is correct; group the rows if you want one line each.
Try it yourself
Add a book by Cleo, run both queries again and predict the results before you look.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
This page is a free outline. No downloadable files, paid content, courses or certificates are offered on this site.