Skip to main content

How to Keep Rows Without a Match in SQLite with LEFT JOIN

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
SELECT <columns>
FROM <table 1>
LEFT JOIN <table 2> ON <condition>;
PartWhat 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 --version to 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:

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;

The left_join.sql script in a macOS-style code window: an authors table with three authors including Mary Shelley, a books table with three books, then the same SELECT of author names and book titles twice, first with JOIN and then with LEFT JOIN.

  • Mary Shelley is an author with no books in the table.
  • The two SELECT statements are the same except for the join: JOIN first, then LEFT JOIN.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
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.

Output
+---------------+------------+
| name | title |
+---------------+------------+
| Jane Austen | Emma |
| Jane Austen | Persuasion |
| Frank Herbert | Dune |
+---------------+------------+
+---------------+------------+
| name | title |
+---------------+------------+
| Jane Austen | Emma |
| Jane Austen | Persuasion |
| Frank Herbert | Dune |
| Mary Shelley | |
+---------------+------------+

The output in a terminal: the JOIN result lists three books by Jane Austen and Frank Herbert, and the LEFT JOIN result adds a fourth row, Mary Shelley with an empty title.

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​

MistakeWhat happensDo 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 truePut 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 authorsMary Shelley wasn't in the result: a LEFT JOIN keeps every row of the first table, and here that's booksPut the table whose rows you want to keep first

Troubleshooting​

SymptomLikely causeFix
Some rows have empty cellsThose rows have no match in the second table, so its columns are NULLThat'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 existsThe script already ran on library.dbDelete 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​