A SQL join combines rows from two tables wherever a condition matches, usually a key such as orders.customer_id = customers.id. INNER JOIN keeps only matching rows, LEFT JOIN also keeps unmatched rows from the left table, RIGHT JOIN keeps unmatched rows from the right table, and FULL OUTER JOIN keeps unmatched rows from both.
Every result below is real output from Python's built-in sqlite3 module (SQLite 3.43.1), so you can paste the setup into any SQLite shell and check it.
The two example tables
CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER, total REAL);
INSERT INTO customers VALUES (1,'Ana'), (2,'Ben'), (3,'Chen');
INSERT INTO orders VALUES (101,1,25.00), (102,1,40.00), (103,2,15.50), (104,4,99.00);
The data is chosen to show every edge case at once. Ana has two orders, Ben has one, Chen has none, and order 104 points at customer 4, who does not exist.
INNER JOIN: only rows that match on both sides
SELECT c.name, o.id, o.total
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id
ORDER BY o.id;
Output:
('Ana', 101, 25.0)
('Ana', 102, 40.0)
('Ben', 103, 15.5)
Chen disappears because no order matches that customer, and order 104 disappears because no customer matches it. Ana appears twice because she matches two orders. PostgreSQL's documentation puts it exactly: for each row of the first table, the result has a row for each row in the second table that satisfies the join condition.
LEFT JOIN: every row from the left table
SELECT c.name, o.id, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
ORDER BY c.id, o.id;
Output:
('Ana', 101, 25.0)
('Ana', 102, 40.0)
('Ben', 103, 15.5)
('Chen', None, None)
A left join first does the inner join, then adds one row for each left-table row that matched nothing, with NULL in the right table's columns (Python prints NULL as None). This is the join you want for "all customers, with their orders if they have any".
RIGHT JOIN: every row from the right table
SELECT c.name, o.id, o.total
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.id
ORDER BY o.id;
Output:
('Ana', 101, 25.0)
('Ana', 102, 40.0)
('Ben', 103, 15.5)
(None, 104, 99.0)
Now the orphan order 104 survives, with NULL for the customer name. A right join is the mirror image of a left join: customers RIGHT JOIN orders returns the same rows as orders LEFT JOIN customers. You can write every outer join as a left join by swapping the table order, which many people find easier to read.
FULL OUTER JOIN: unmatched rows from both sides
SELECT c.name, o.id, o.total
FROM customers c
FULL OUTER JOIN orders o ON o.customer_id = c.id
ORDER BY c.id, o.id;
Output:
(None, 104, 99.0)
('Ana', 101, 25.0)
('Ana', 102, 40.0)
('Ben', 103, 15.5)
('Chen', None, None)
Both Chen and order 104 are kept. A full join is useful for reconciliation: comparing two lists and seeing what exists only on one side. The orphan row sorts first here because, as the output shows, SQLite puts NULL before any number when sorting ascending by c.id. Add NULLS LAST to the ORDER BY term if you want it at the end.
SQLite only gained RIGHT JOIN and FULL OUTER JOIN in version 3.39.0 (June 2022). If python -c "import sqlite3; print(sqlite3.sqlite_version)" prints something older, those two queries fail and you need to rewrite them as left joins.
CROSS JOIN: every combination
SELECT COUNT(*) FROM customers CROSS JOIN orders;
Output: (12,). A cross join has no condition and pairs every row with every row: 3 customers × 4 orders = 12 rows. It is occasionally useful (every size × every colour), but it is far more often an accident. Forgetting the ON clause in old comma syntax, FROM customers, orders, produces the same 12 rows.
Join types side by side
| Join | Keeps unmatched left rows | Keeps unmatched right rows | Rows in this example |
|---|---|---|---|
INNER JOIN | No | No | 3 |
LEFT JOIN | Yes | No | 4 (adds Chen) |
RIGHT JOIN | No | Yes | 4 (adds order 104) |
FULL OUTER JOIN | Yes | Yes | 5 |
CROSS JOIN | No condition; every pair | 12 | |
The ON vs WHERE trap with LEFT JOIN
This is a classic outer-join bug. Suppose you want every customer, plus only their orders over 20. Putting the filter in WHERE silently turns the left join back into an inner join:
SELECT c.name, o.id, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.total > 20;
Output:
('Ana', 101, 25.0)
('Ana', 102, 40.0)
Ben and Chen are gone. Their rows had o.total equal to 15.5 and NULL, and NULL > 20 is not true, so WHERE removed them. Move the condition into the ON clause instead:
SELECT c.name, o.id, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id AND o.total > 20;
Output:
('Ana', 101, 25.0)
('Ana', 102, 40.0)
('Ben', None, None)
('Chen', None, None)
The PostgreSQL manual explains why: a restriction in ON is processed before the join, while a restriction in WHERE is processed after it. For inner joins the two give the same result. For outer joins they don't.
Find rows with no match (anti-join)
"Customers who never ordered" is a left join plus a NULL check on a column that can't be NULL in a real match, such as the right table's primary key:
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;
Output: ('Chen',). NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id) returns the same answer and some people find it reads more clearly.
Counting with a LEFT JOIN: COUNT(*) vs COUNT(column)
SELECT c.name, COUNT(o.id) AS orders, COALESCE(SUM(o.total), 0) AS spent
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id;
Output:
('Ana', 2, 65.0)
('Ben', 1, 15.5)
('Chen', 0, 0)
Swap COUNT(o.id) for COUNT(*) and Chen shows 1 order, because COUNT(*) counts the NULL-filled row the left join added. COUNT(column) skips NULLs, so count a column from the right table. COALESCE turns Chen's NULL sum into 0.
Self join and ambiguous column names
A table can be joined to itself by giving it two aliases. This lists every pair of customers once:
SELECT a.name, b.name FROM customers a JOIN customers b ON a.id < b.id;
Output: ('Ana', 'Ben'), ('Ana', 'Chen'), ('Ben', 'Chen'). Aliases also fix a frequent join error. Both tables here have an id column, so SELECT name, id FROM customers c JOIN orders o ON o.customer_id = c.id fails in SQLite with:
sqlite3.OperationalError: ambiguous column name: id
Write c.id or o.id to say which one you mean.
USING and NATURAL JOIN
When both tables name the key column the same, JOIN orders USING (customer_id) is shorthand for an equality on that column, and the column appears only once in the output. NATURAL JOIN goes further and joins on every column name the two tables share. Avoid it in real code: adding an unrelated column such as created_at to both tables silently changes the join condition; the PostgreSQL manual calls NATURAL considerably more risky than USING for exactly this reason. If the tables share no column names, PostgreSQL's documentation notes that a natural join behaves like a cross join.
If your join returns the wrong rows
Run the query with SELECT * and no GROUP BY first, so you can see the raw joined rows. Too many rows means the right table has several matches per key; too few means an inner join, or a WHERE on the right table, is dropping unmatched rows. Joins on columns with different types or stray whitespace can also fail to match without any error. When the join feeds a web form or API, build it with parameters rather than string concatenation; see SQL injection explained for why.
Frequently asked questions
- Is JOIN the same as INNER JOIN?
- Yes. A bare JOIN is an inner join; the PostgreSQL documentation notes that the words INNER and OUTER are optional in all forms, with INNER as the default. Likewise LEFT JOIN and LEFT OUTER JOIN are the same thing. SQLite behaves the same way.
- Why does my LEFT JOIN return more rows than the left table has?
- A LEFT JOIN keeps at least one row per left-table row, but it produces one row for every match. If a customer has three orders, that customer appears three times. Duplicated rows after a join almost always mean the right table has several matching rows per key.
- Does the order of tables matter in a join?
- For INNER JOIN and FULL OUTER JOIN the result rows are the same whichever table comes first; only the column order changes. For LEFT and RIGHT JOIN it matters, because the side you name decides which table keeps its unmatched rows. A LEFT JOIN B returns the same rows as B RIGHT JOIN A.
- Does SQLite support RIGHT JOIN and FULL OUTER JOIN?
- Yes, since SQLite 3.39.0, released on 2022-06-25. Older SQLite versions only support INNER, LEFT and CROSS joins, so on those you swap the table order to turn a RIGHT JOIN into a LEFT JOIN.