SQL joins cheat sheet

Every kind of join on one printable page, with the patterns built on them: counting with a LEFT JOIN, finding rows with no match, and joining a table to itself. Every snippet runs on both PostgreSQL and SQLite.

Practise in 5 minutes
sqlstreakSQL joins cheat sheetPostgreSQL & SQLite · tested on every release · sqlstreak.com

The join types

SELECT o.id, c.name
FROM orders o
JOIN customers c ON c.id = o.customer_id;

INNER JOIN (JOIN): only rows that match.

SELECT c.name, o.id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;

LEFT JOIN: every left row; NULLs where no match.

SELECT c.name, o.id
FROM orders o
RIGHT JOIN customers c ON c.id = o.customer_id;

RIGHT JOIN: every right row (SQLite 3.39+).

SELECT c.name, o.id
FROM customers c
FULL OUTER JOIN orders o ON o.customer_id = c.id;

FULL JOIN: every row from both sides.

SELECT s.name, m.name
FROM staff s CROSS JOIN menu_items m
WHERE s.role = 'chef' AND m.category = 'pizza';

CROSS JOIN: every combination.

Joining more

SELECT o.id, m.name, oi.quantity
FROM orders o
JOIN order_items oi ON oi.order_id = o.id
JOIN menu_items m ON m.id = oi.item_id;

Chain joins: one ON per table.

SELECT o.id, c.name
FROM orders o
JOIN customers c ON c.id = o.customer_id
  AND o.status = 'delivered';

Extra conditions can live in ON.

SELECT * FROM order_items
JOIN menu_items ON menu_items.id = order_items.item_id;

Without aliases, prefix columns with the table name.

Joins with GROUP BY

SELECT m.name, SUM(oi.quantity) AS sold
FROM order_items oi
JOIN menu_items m ON m.id = oi.item_id
GROUP BY m.name
ORDER BY sold DESC;

Join, then aggregate.

SELECT c.name, COUNT(o.id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name;

Count a right-hand column so no match = 0.

SELECT c.city, SUM(oi.quantity * m.price) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
JOIN menu_items m ON m.id = oi.item_id
GROUP BY c.city;

Revenue per city across four tables.

Match or no match

SELECT c.name FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

Anti-join: rows with no match.

SELECT c.name FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o
                  WHERE o.customer_id = c.id);

The same with NOT EXISTS (safe with NULLs).

SELECT c.name FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o
              WHERE o.customer_id = c.id
                AND o.status = 'cancelled');

Semi-join: rows with at least one match, each once.

Self joins

SELECT e.name, m.name AS manager
FROM staff e
LEFT JOIN staff m ON m.id = e.manager_id;

Two aliases of one table: employee and manager.

SELECT a.name, b.name
FROM staff a
JOIN staff b ON a.city = b.city AND a.id < b.id;

Pairs in the same city, each pair once.

Join traps

SELECT id FROM orders o
JOIN customers c ON c.id = o.customer_id;

Ambiguous column: write o.id.

SELECT c.name, o.id FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'delivered';

A WHERE on the right table undoes the LEFT JOIN: move it to ON.

SELECT COUNT(*) FROM customers c
JOIN orders o ON o.customer_id = c.id;

Joins repeat rows: this counts orders, not customers.

More cheat sheets

Learn it properly

Read the SQL JOINs explained guide, or practise in “Your first JOIN” (Pro): short exercises with instant feedback.