Skip to main content

How to Join Two Tables in SQLite with JOIN

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
SELECT <columns>
FROM <table 1>
JOIN <table 2> ON <table 2>.<column> = <table 1>.<column>;
PartWhat 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 --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 join_tables.sql:

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;

The join_tables.sql script in a macOS-style code window: an authors table with author_id and name, a books table with title and author_id, two authors, three books, and a SELECT of books.title and authors.name from books joined to authors on author_id.

  • authors holds each author once, with a number in author_id.
  • Each book stores its author's number, not the name.
  • The ON condition pairs each book with the author whose number it stores.

Step 2: Run It​

In the folder that holds the script, run:

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

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

The output in a terminal: Emma and Persuasion next to Jane Austen, and Dune next to 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​

MistakeWhat happensDo this instead
Leaving out ONWith three authors and three books, SELECT count(*) FROM books JOIN authors returned 9: every book paired with every authorSay which rows belong together with ON
A column both tables have, without its table, as in SELECT title, author_idambiguous column name: author_idName its table: books.author_id

Troubleshooting​

SymptomLikely causeFix
An author with no books is missing from the resultJOIN keeps only rows that have a match in both tablesUse LEFT JOIN, which keeps every row of the first table
Parse error near line 1 of join_tables.sql: table authors already existsThe script already ran on library.dbDelete 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​