How to Keep Rows Without a Match in SQLite with LEFT JOIN
JOIN drops a row that has no match in the other table, such as an author with no books yet. LEFT JOIN keeps every row of the first table and fills the missing columns with NULL. This guide runs both on the same tables, with real output from SQLite 3.54.1.
Syntax
SELECT <columns>
FROM <table 1>
LEFT JOIN <table 2> ON <condition>;
| Part | What it means |
|---|---|
FROM <table 1> | The table whose rows are all kept |
LEFT JOIN <table 2> | The table whose matching rows are added to them |
ON <condition> | Which rows belong together |
A row of the first table with no match comes back once, with NULL in the second table's columns. SQLite's documentation on joins: "If the join-operator is a "LEFT JOIN" or "LEFT OUTER JOIN", then after the ON or USING filtering clauses have been applied, an extra row is added to the output for each row in the original left-hand input dataset that does not match any row in the right-hand dataset."
Prerequisites
- The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run
sqlite3 --versionto see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.
Step 1: Write the Script
Save this as left_join.sql:
CREATE TABLE authors (
author_id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE books (
title TEXT NOT NULL,
author_id INTEGER NOT NULL
);
INSERT INTO authors (author_id, name) VALUES (1, 'Jane Austen');
INSERT INTO authors (author_id, name) VALUES (2, 'Frank Herbert');
INSERT INTO authors (author_id, name) VALUES (3, 'Mary Shelley');
INSERT INTO books (title, author_id) VALUES ('Emma', 1);
INSERT INTO books (title, author_id) VALUES ('Persuasion', 1);
INSERT INTO books (title, author_id) VALUES ('Dune', 2);
SELECT authors.name, books.title
FROM authors
JOIN books ON books.author_id = authors.author_id;
SELECT authors.name, books.title
FROM authors
LEFT JOIN books ON books.author_id = authors.author_id;

- Mary Shelley is an author with no books in the table.
- The two
SELECTstatements are the same except for the join:JOINfirst, thenLEFT JOIN.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table library.db ".read left_join.sql"
sqlite3 opens the database file library.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.
+---------------+------------+
| name | title |
+---------------+------------+
| Jane Austen | Emma |
| Jane Austen | Persuasion |
| Frank Herbert | Dune |
+---------------+------------+
+---------------+------------+
| name | title |
+---------------+------------+
| Jane Austen | Emma |
| Jane Austen | Persuasion |
| Frank Herbert | Dune |
| Mary Shelley | |
+---------------+------------+

JOIN left Mary Shelley out, because no book matches her. LEFT JOIN kept her, with an empty title: her row has NULL there, and the shell prints NULL as nothing.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A WHERE on the second table, as in WHERE books.title != 'Emma' | Mary Shelley disappeared again: her title is NULL, and a comparison with NULL isn't true | Put the condition in ON: ON books.author_id = authors.author_id AND books.title != 'Emma' kept her |
The tables the other way round, FROM books LEFT JOIN authors | Mary Shelley wasn't in the result: a LEFT JOIN keeps every row of the first table, and here that's books | Put the table whose rows you want to keep first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Some rows have empty cells | Those rows have no match in the second table, so its columns are NULL | That's what LEFT JOIN does. Use JOIN if you want only rows with a match |
Parse error near line 1 of left_join.sql: table authors already exists | The script already ran on library.db | Delete library.db and run the script again |
FAQ
How do I find the rows that have no match?
Keep only the rows where the second table's column is NULL: FROM authors LEFT JOIN books ON books.author_id = authors.author_id WHERE books.title IS NULL returned Mary Shelley alone.
Is LEFT JOIN the same as LEFT OUTER JOIN?
Yes. LEFT OUTER JOIN in place of LEFT JOIN returned the same four rows, and the documentation quoted above names them together.
Conclusion
LEFT JOIN kept Mary Shelley, the author with no books, with NULL where a title would be, while JOIN had left her out.
Additional Resources
- FROM clause processing — how a
LEFT JOINadds the rows without a match. - SELECT — the full statement.
