How to Join Two Tables in SQLite with JOIN
A table of books that stores only each author's number can't show the author's name by itself. JOIN pairs each row with the matching row of another table, so one result can show columns from both. This guide joins books to their authors, with real output from SQLite 3.54.1.
Syntax
SELECT <columns>
FROM <table 1>
JOIN <table 2> ON <table 2>.<column> = <table 1>.<column>;
| Part | What it means |
|---|---|
FROM <table 1> | The first table |
JOIN <table 2> | The table whose rows are paired with it |
ON <condition> | Which rows belong together, usually a key in one table equal to the same key in the other |
<table>.<column> | A column named together with its table, so SQLite knows which one you mean when both tables have it |
SQLite's documentation on joins: "If there is an ON clause then the ON expression is evaluated for each row of the cartesian product as a boolean expression. Only rows for which the expression evaluates to true are included from the dataset." In plain words, SQLite pairs every row of the first table with every row of the second, and keeps only the pairs for which the ON condition is true.
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 join_tables.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 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 books.title, authors.name
FROM books
JOIN authors ON authors.author_id = books.author_id;

authorsholds each author once, with a number inauthor_id.- Each book stores its author's number, not the name.
- The
ONcondition pairs each book with the author whose number it stores.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table library.db ".read join_tables.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.
+------------+---------------+
| title | name |
+------------+---------------+
| Emma | Jane Austen |
| Persuasion | Jane Austen |
| Dune | Frank Herbert |
+------------+---------------+

Each book came back with its author's name, which lives in the other table. Emma and Persuasion both store author 1, so Jane Austen appears twice.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out ON | With three authors and three books, SELECT count(*) FROM books JOIN authors returned 9: every book paired with every author | Say which rows belong together with ON |
A column both tables have, without its table, as in SELECT title, author_id | ambiguous column name: author_id | Name its table: books.author_id |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| An author with no books is missing from the result | JOIN keeps only rows that have a match in both tables | Use LEFT JOIN, which keeps every row of the first table |
Parse error near line 1 of join_tables.sql: table authors already exists | The script already ran on library.db | Delete library.db and run the script again |
FAQ
Is JOIN the same as INNER JOIN?
Yes, INNER JOIN is its longer name. FROM books INNER JOIN authors USING (author_id) returned the same three rows as the script.
What does USING do?
It's a shorter ON for a column that has the same name in both tables: USING (author_id) pairs the rows whose author_id values are equal. The documentation: "For each pair of named columns, the expression "lhs.X = rhs.X" is evaluated for each row of the cartesian product as a boolean expression."
Conclusion
JOIN authors ON authors.author_id = books.author_id paired each book with its author, so one result showed titles from one table next to names from the other.
Additional Resources
- FROM clause processing — how SQLite pairs rows in a join.
- SELECT — the full statement.
