Joining tables
Introduction
SQL joins combine rows from tables. Match columns with ON or USING, or pair rows with CROSS JOIN. Examples use the sample tables below.
- PostgreSQL join tutorial (postgresql.org)
- SQLite SELECT and joins (sqlite.org)
- MySQL JOIN syntax (dev.mysql.com)
Match rows in both tables
SELECT c.name, o.order_id, o.total
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id;
Result
name order_id total
---- -------- -----
Ada 101 25
Ada 102 40
Bob 103 15
JOIN alone means INNER JOIN: unmatched rows are dropped.
How joins overlap
Query
SELECT * FROM a
JOIN b ON b.a_id = a.id;
Overlap
┌────────┐
│ a ┌───┼────┐
│ │ ∩ │ │
└────┼───┘ b │
└────────┘
The overlap is an inner join; add one circle's exclusive rows for a left or right join.
See: SQLite joins
Outer joins
Left join
SELECT c.name, o.order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id;
Result
name order_id total
---- -------- -----
Ada 101 25
Ada 102 40
Bob 103 15
Cy NULL NULL
Every left row survives; missing right columns are NULL.
See: SQLite joins
Right join
SELECT c.name, o.order_id
FROM customers AS c
RIGHT JOIN orders AS o
ON o.customer_id = c.id;
Result
name order_id
---- --------
Ada 101
Ada 102
Bob 103
NULL 104
Every right row survives, including order 104 with no customer.
See: MySQL JOIN syntax
Full outer join
SELECT c.name, o.order_id
FROM customers AS c
FULL OUTER JOIN orders AS o
ON o.customer_id = c.id;
Result
name order_id
---- --------
Ada 101
Ada 102
Bob 103
Cy NULL
NULL 104
Both sides survive; unmatched columns are NULL.
Join variations
Cross join
SELECT COUNT(*)
FROM customers CROSS JOIN orders;
Result
COUNT(*)
--------
12
Every left row pairs with every right row: 3 × 4 = 12.
See: SQLite joins
Self join
SELECT e.name AS employee,
m.name AS manager
FROM employees AS e
JOIN employees AS m
ON m.id = e.manager_id;
Result
employee manager
-------- -------
Bob Ada
Cy Ada
Dee Bob
Alias the same table twice to match rows within it; employees with no manager (Ada) are dropped.
Join three tables
SELECT c.name, o.order_id, s.status
FROM customers AS c
JOIN orders AS o
ON o.customer_id = c.id
JOIN shipments AS s
ON s.order_id = o.order_id;
Result
name order_id status
---- -------- -------
Ada 101 shipped
Bob 103 pending
Each JOIN adds a table; conditions can reference earlier ones.
Common patterns
Find unmatched rows
SELECT c.name
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
WHERE o.order_id IS NULL;
Result
name
----
Cy
A left join plus IS NULL finds customers without orders.
Filter in the ON clause
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
AND o.total > 30;
Result
name order_id
---- --------
Ada 102
Bob NULL
Cy NULL
Conditions in ON keep unmatched left rows; WHERE would drop them.
Emulate a full join
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
UNION ALL
SELECT c.name, o.order_id
FROM customers AS c
RIGHT JOIN orders AS o
ON o.customer_id = c.id
WHERE c.id IS NULL;
Result
name order_id
---- --------
Ada 101
Ada 102
Bob 103
Cy NULL
NULL 104
MySQL has no FULL OUTER JOIN; union a left and a right join.
See: MySQL UNION syntax
Dialect notes
SQLite
SELECT sqlite_version();
Result
3.54.0
RIGHT and FULL OUTER JOIN require SQLite 3.39 or newer.
PostgreSQL
SELECT order_id, s.status
FROM orders AS o
FULL OUTER JOIN shipments AS s
USING (order_id);
Result
order_id status
-------- -------
101 shipped
102 NULL
103 pending
104 NULL
105 lost
Full joins are native; USING keeps one merged join column.
MySQL
SELECT c.name, COUNT(o.order_id) AS orders
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
GROUP BY c.name;
Result
name orders
---- ------
Ada 2
Bob 1
Cy 0
JOIN, LEFT, and RIGHT work; emulate FULL with UNION ALL.
See: MySQL JOIN syntax
Reference
Join types
| Join | Keeps |
|---|---|
INNER JOIN | Matching rows from both |
LEFT JOIN | All left rows |
RIGHT JOIN | All right rows |
FULL OUTER JOIN | All rows from both |
CROSS JOIN | Every pair of rows |
USING clause
SELECT o.order_id, s.status
FROM orders AS o
JOIN shipments AS s USING (order_id);
Result
order_id status
-------- -------
101 shipped
103 pending
USING merges the shared column into one output column.
Sample data
Tables
CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE orders (order_id INTEGER PRIMARY KEY,
customer_id INTEGER, total INTEGER);
CREATE TABLE shipments (order_id INTEGER, status TEXT);
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT,
manager_id INTEGER);
Rows
INSERT INTO customers VALUES (1, 'Ada'), (2, 'Bob'), (3, 'Cy');
INSERT INTO orders VALUES (101, 1, 25), (102, 1, 40),
(103, 2, 15), (104, 4, 10);
INSERT INTO shipments VALUES (101, 'shipped'),
(103, 'pending'), (105, 'lost');
INSERT INTO employees VALUES (1, 'Ada', NULL), (2, 'Bob', 1),
(3, 'Cy', 1), (4, 'Dee', 2);
All examples above use these four tables.
See: SQLite CREATE TABLE
0 Comments for this cheatsheet. Write yours!