SQL Joins Cheat Sheet
A searchable, printable SQL joins reference — inner, left, right, full, anti and semi joins, self joins, ON vs WHERE and LATERAL. Free.
Basic join types
9SELECT * FROM a INNER JOIN b ON a.id = b.a_id;
SELECT * FROM a LEFT JOIN b ON a.id = b.a_id;
SELECT * FROM a RIGHT JOIN b ON a.id = b.a_id;
SELECT * FROM a FULL OUTER JOIN b ON a.id = b.a_id;
JOIN = INNER JOIN
LEFT OUTER JOIN = LEFT JOIN
LEFT JOIN ... UNION ... RIGHT JOIN ...
ON a.id = b.a_id AND a.type = b.type
ON a.qty BETWEEN p.min_qty AND p.max_qty
Anti-joins (no match)
8LEFT JOIN b ON a.id = b.a_id WHERE b.a_id IS NULL
WHERE NOT EXISTS (SELECT 1 FROM b WHERE b.a_id = a.id)
WHERE a.id NOT IN (SELECT a_id FROM b)
WHERE a.id NOT IN (SELECT a_id FROM b WHERE a_id IS NOT NULL)
RIGHT JOIN a ... WHERE a.id IS NULL
SELECT id FROM a EXCEPT SELECT a_id FROM b;
users without orders = LEFT JOIN + IS NULL
Prefer NOT EXISTS over NOT IN
Semi-joins (has a match)
8WHERE EXISTS (SELECT 1 FROM b WHERE b.a_id = a.id)
WHERE a.id IN (SELECT a_id FROM b)
JOIN b ... GROUP BY a.id
SELECT DISTINCT a.* FROM a JOIN b ON ...
EXISTS stops at the first match
EXISTS (SELECT 1 ...) vs (SELECT * ...)
IN: uncorrelated / EXISTS: correlated
users WITH orders = EXISTS subquery
Self joins
8FROM emp e JOIN emp m ON e.manager_id = m.id
FROM emp e LEFT JOIN emp m ON e.manager_id = m.id
SELECT e.name, m.name AS manager FROM ...
ON a.id < b.id
ON a.id <> b.id
ON b.day = a.day + INTERVAL 1 DAY
ON a.city = b.city AND a.id <> b.id
WITH RECURSIVE tree AS (... UNION ALL ...)
Cross & multi-table joins
8SELECT * FROM sizes CROSS JOIN colors;
FROM sizes, colors
FROM users u JOIN orders o ON ... JOIN items i ON ...
JOIN dates d CROSS JOIN users u LEFT JOIN ...
u LEFT JOIN o ON ... LEFT JOIN i ON i.o_id = o.id
u LEFT JOIN o ... INNER JOIN i ON i.o_id = o.id
FROM a JOIN (b LEFT JOIN c ON ...) ON ...
Written order is logical, not execution order
ON vs WHERE (the gotcha)
8LEFT JOIN b ON a.id = b.a_id AND b.status = 1
LEFT JOIN b ON a.id = b.a_id WHERE b.status = 1
WHERE b.a_id IS NULL OR b.status = 1
LEFT JOIN b ON ... AND a.type = 'x'
WHERE a.type = 'x'
INNER JOIN: ON and WHERE are equivalent
COALESCE(b.total, 0)
COUNT(b.id) vs COUNT(*)
USING, NATURAL & set ops
9JOIN b USING (user_id)
USING outputs ONE shared column
NATURAL JOIN b
Avoid NATURAL JOIN in production
SELECT ... UNION SELECT ...
UNION ALL
JOIN adds columns, UNION adds rows
INTERSECT
UNION needs same column count and types
LATERAL & performance
9CROSS JOIN LATERAL (SELECT ... WHERE b.a_id = a.id LIMIT 3) t
LEFT JOIN LATERAL (...) t ON TRUE
CROSS APPLY / OUTER APPLY
LATERAL sees columns of preceding tables
CREATE INDEX idx ON orders (user_id);
EXPLAIN SELECT ...;
Filter early: join the smallest set first
ON DATE(a.created_at) = b.day
Join on same-type, same-collation columns
No entry matches “:q”.
About SQL Joins Cheat Sheet
This SQL joins cheat sheet does one thing thoroughly: it explains every way to combine tables. Eight sections cover the basic join types, anti-joins for rows with no match, semi-joins for rows that do have a match, self joins, cross and multi-table joins, the classic ON versus WHERE gotcha, USING, NATURAL and the set operations, and LATERAL joins with performance notes.
Joins are where SQL results quietly go wrong — a LEFT JOIN filtered in the WHERE clause silently becomes an INNER JOIN, and NOT IN with a NULL returns nothing. Those traps get their own rows here, next to the correct pattern to use instead.
Like every cheat sheet in this group it is free and client-side: filter rows live with the search box, hop between sections with the sticky table of contents, copy any statement with one click and print the page for reference at your desk.
How to use SQL Joins Cheat Sheet
- Open the sheet and review the sections, from Basic join types to LATERAL & performance.
- Search for a keyword such as LEFT JOIN, EXISTS or NOT IN to filter every row live.
- Read the ON vs WHERE section before debugging a LEFT JOIN that returns too few rows.
- Click a statement or its copy icon to copy the SQL to your clipboard.
- Use Print for a paper copy of the full joins reference.