SQL JOIN Cheat Sheet
Every JOIN type with diagrams, syntax, and when to use each one.
Updated
The JOIN types at a glance#
Sample data used below#
customers: Asha (1), Ravi (2), Meera (3), John (4)
orders
Meera and John have no orders; order 104 points to a customer that does not exist.
INNER JOIN: only matches#
SELECT c.name, o.order_id
FROM customers c
INNER JOIN orders o ON o.customer_id = c.customer_id;name | order_id
-----+---------
Asha | 101
Asha | 102
Ravi | 103LEFT JOIN: all rows from the left table#
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id;name | order_id
------+---------
Asha | 101
Asha | 102
Ravi | 103
Meera | NULL
John | NULLRIGHT JOIN: all rows from the right table#
SELECT c.name, o.order_id
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.customer_id;
-- same rows as: FROM orders o LEFT JOIN customers c ON ...name | order_id
-----+---------
Asha | 101
Asha | 102
Ravi | 103
NULL | 104FULL OUTER JOIN: everything#
SELECT c.name, o.order_id
FROM customers c
FULL OUTER JOIN orders o ON o.customer_id = c.customer_id;name | order_id
------+---------
Asha | 101
Asha | 102
Ravi | 103
Meera | NULL
John | NULL
NULL | 104MySQL has no FULL OUTER JOIN. Emulate it:
SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id
UNION
SELECT c.name, o.order_id FROM customers c RIGHT JOIN orders o ON o.customer_id = c.customer_id;Find rows with no match (anti-join)#
-- Customers who never ordered
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
-- Same result, often clearer
SELECT c.name
FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);name
-----
Meera
JohnAvoid NOT IN (SELECT customer_id FROM orders): one NULL in the subquery makes it return no rows.
Self join: a table joined to itself#
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;employee | manager
---------+--------
Asha | Priya
Ravi | Meera
Meera | NULL
John | Priya
Priya | NULLTo pair rows that share a value without duplicates, add AND a.id < b.id to the join condition.
CROSS JOIN: every combination#
SELECT s.size, c.color
FROM sizes s
CROSS JOIN colors c; -- rows = rows(sizes) × rows(colors)Joining three or more tables#
SELECT o.order_id, c.name, p.name AS product, oi.quantity
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
JOIN order_items oi ON oi.order_id = o.order_id
JOIN products p ON p.product_id = oi.product_id;Each new table needs its own ON condition linking it to a table already in the query.
ON vs WHERE in a LEFT JOIN#
-- Condition in ON: every customer kept
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id AND o.amount > 200;name | order_id
------+---------
Asha | 101
Ravi | 103
Meera | NULL
John | NULL-- Condition in WHERE: unmatched customers disappear
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.amount > 200;name | order_id
-----+---------
Asha | 101
Ravi | 103Rule: in a LEFT JOIN, filters on the right table go in ON; filters on the left table go in WHERE.
Traps to avoid#
Full lessons: Introduction to JOINs, INNER, LEFT, RIGHT, FULL OUTER, SELF, CROSS, Multiple JOINs.
Want the full explanations? Go to the Learn SQL course →