SQL Joins Cheat Sheet
A join is about rows, not circles: count the matches on each key and you can predict every result.

What the sheet covers
Worked examples on two tiny tables so you can count the result of INNER, LEFT, RIGHT, FULL and CROSS JOIN (FULL is not MySQL). For a matching key, a join returns left rows times right rows: the fan-out that double-counts totals unless you aggregate first. To keep unmatched left rows, put a null-rejecting right-table condition in ON, not WHERE; in WHERE it can turn a LEFT JOIN into an INNER JOIN. EXISTS / NOT EXISTS keep each left row once (they are not multiplied by the right table). NULL keys never match under = ; use IS NOT DISTINCT FROM (or MySQL <=>) when you need NULL to match NULL.
Topics on the sheet
- Two tiny tables
- One query, four join types
- The fan-out check
- LEFT JOIN: filter in ON or in WHERE?
- Semi and anti joins, and NULL keys
- Which join do I need?
Download the PDF
JOIN types, row by row as a one-page PDF in Letter and A4, plus all six sheets in one SQL cheat sheet PDF. Free PDF, download it right after you sign up. Signing up joins the dataengineers.club newsletter and unlocks the PDF bundle in Letter and A4. Unsubscribe anytime.
Two tiny tables
Table c (customers) and table o (orders). Ana has no order, Cy has two, order 13 has no customer.
| cid | name | oid | cid |
|---|---|---|---|
| 1 | Ana | 10 | 2 |
| 2 | Ben | 11 | 3 |
| 3 | Cy | 12 | 3 |
| 13 | 4 |
One query, four join types
Rows: INNER 3, LEFT 4, RIGHT 4 (a LEFT JOIN with the tables swapped), FULL 5. CROSS JOIN: no ON, every pair, 3 x 4 = 12.
| c.cid | name | oid | o.cid | kept by |
|---|---|---|---|---|
| 1 | Ana | NULL | NULL | LEFT, FULL |
| 2 | Ben | 10 | 2 | all four |
| 3 | Cy | 11 | 3 | all four |
| 3 | Cy | 12 | 3 | all four |
| NULL | NULL | 13 | 4 | RIGHT, FULL |
SELECT c.cid, c.name, o.oid, o.cid
FROM c
[INNER|LEFT|RIGHT|FULL] JOIN o
ON o.cid = c.cid;The fan-out check
For a matching key, a join returns left rows x right rows. Cy has 2 orders, so Cy comes back twice and any sum over c counts Cy twice. Is the key unique? On o, GROUP BY cid HAVING COUNT(*) > 1 returns cid 3.
-- fix: aggregate first, then join
WITH per_c AS (
SELECT cid, COUNT(*) AS orders
FROM o GROUP BY cid
)
SELECT c.cid, c.name,
COALESCE(p.orders, 0) AS orders
FROM c
LEFT JOIN per_c p
ON p.cid = c.cid;Habit: COUNT(*) before and after every join.
LEFT JOIN: filter in ON or in WHERE?
SELECT c.cid, o.oid
FROM c LEFT JOIN o
ON o.cid = c.cid
AND o.oid > 10; -- A: in ON
-- B: WHERE o.oid > 10 instead- A, in ON: 4 rows (Ana and Ben stay, with a NULL oid).
- B, in WHERE: 2 rows (only Cy's orders 11 and 12).
- In WHERE, NULL > 10 is not true: unmatched rows drop out and the LEFT JOIN acts like INNER. To keep unmatched left rows, put a null-rejecting right-table condition in ON, not WHERE.
Semi and anti joins, and NULL keys
SELECT * FROM c
WHERE EXISTS ( -- Ben, Cy
SELECT 1 FROM o
WHERE o.cid = c.cid);
-- NOT EXISTS: Ana (anti join)- Keep each c row that has (or lacks) a match; it is not multiplied by the right table: EXISTS (Ben, Cy) and NOT EXISTS (Ana).
- Anti join as LEFT JOIN: WHERE o.oid IS NULL (test a column that is never NULL in o). Avoid NOT IN: one NULL returns no rows (NULL handling).
- NULL = NULL is not true, so NULL keys never match. IS NOT DISTINCT FROM: DuckDB, PostgreSQL, Snowflake, BigQuery, SQL Server 2022. MySQL: a.k <=> b.k.
Which join do I need?
| You need | Use |
|---|---|
| Only rows that match | INNER |
| All left rows, matches if any | LEFT |
| Has / has no match, no duplicates | EXISTS / NOT EXISTS |
| Compare two tables, gaps on both sides | FULL (not MySQL) |
| Every combination (dates x stores) | CROSS |
Engines named on the sheet
DuckDB, PostgreSQL, Snowflake, BigQuery, SQL Server, MySQL. IS NOT DISTINCT FROM is noted for DuckDB, PostgreSQL, Snowflake, BigQuery and SQL Server 2022; MySQL uses <=>. DuckDB also has SEMI JOIN and ANTI JOIN. DuckDB 2.0.1 is the runtime used in the book. Do not read this list as a claim that every engine was executed for every example.
FAQ
How many rows does a SQL join return?
For a matching key, a join returns left rows times right rows. On the sheet's two tables: INNER 3, LEFT 4, RIGHT 4, FULL 5 (FULL JOIN is not available in MySQL), and CROSS JOIN 3 x 4 = 12.
In a LEFT JOIN, does a filter on the right table go in ON or WHERE?
To keep unmatched left rows, put a null-rejecting right-table condition such as o.oid > 10 in ON, not WHERE. In WHERE, NULL > 10 is not true, so unmatched rows drop out and the LEFT JOIN acts like an INNER JOIN. The anti join is the exception: it tests WHERE o.oid IS NULL on purpose.
Why avoid NOT IN for an anti join?
If the subquery returns one NULL, NOT IN returns no rows. Use NOT EXISTS, or a LEFT JOIN with WHERE on a column that is never NULL in the right table.
Do NULL keys match in a join?
No. NULL = NULL is not true, so NULL keys never match. Use IS NOT DISTINCT FROM (DuckDB, PostgreSQL, Snowflake, BigQuery, SQL Server 2022) or a.k <=> b.k in MySQL.