JOINs are easier to remember as a set than one at a time. They all answer the same question, "which rows from table A go with which rows from table B?", and differ only in what they do with rows that find no partner. This cheat sheet puts them side by side on one small example.
The example
customers: (1, Amina), (2, Karim), (3, Sara)
orders: (101, customer 1), (102, customer 1), (103, customer 2), (104, customer 9)
Sara has no orders. Order 104 points to a customer that does not exist. Every join below uses ON o.customer_id = c.id, with customers on the left and orders on the right.
Picture two overlapping circles: the left circle is customers, the right one is orders, and the overlap is the matched pairs. Each join is a different shaded area. (The circle picture is a memory aid; remember that a match can produce several rows, as Amina's two orders show.)
The joins at a glance
INNER JOIN: the overlap. Only matched pairs. Returns Amina-101, Amina-102, Karim-103. Sara and order 104 are dropped.
LEFT JOIN: the whole left circle. Every customer, plus matching orders. Returns the three matches and Sara with NULL order columns.
RIGHT JOIN: the whole right circle. Every order, plus matching customers. Returns the three matches and order 104 with a NULL customer. Usually written as a LEFT JOIN with the tables swapped.
FULL OUTER JOIN: both circles. Matches, plus Sara with NULL orders, plus order 104 with a NULL customer. Not supported natively in MySQL.
LEFT anti join: left circle minus the overlap. Customers with no orders: Sara. Written as LEFT JOIN ... WHERE o.id IS NULL or, more clearly, NOT EXISTS.
CROSS JOIN: no circles, every pair. Three customers times four orders gives twelve rows. No ON condition.
Side by side
| Join | Keeps unmatched left | Keeps unmatched right | Rows here | Typical question |
|---|---|---|---|---|
| INNER | no | no | 3 | orders with their customer |
| LEFT | yes | no | 4 | all customers, orders if any |
| RIGHT | no | yes | 4 | all orders, customer if any |
| FULL OUTER | yes | yes | 5 | reconcile two lists |
| LEFT anti | only unmatched | no | 1 | customers who never ordered |
| CROSS | n/a | n/a | 12 | every combination |
The syntax in one block
SELECT ... FROM customers c INNER JOIN orders o ON o.customer_id = c.id;
SELECT ... FROM customers c LEFT JOIN orders o ON o.customer_id = c.id;
SELECT ... FROM customers c RIGHT JOIN orders o ON o.customer_id = c.id;
SELECT ... FROM customers c FULL JOIN orders o ON o.customer_id = c.id;
SELECT ... FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
SELECT ... FROM sizes CROSS JOIN colors;
JOIN alone means INNER JOIN, and LEFT JOIN is short for LEFT OUTER JOIN.
Traps that apply to all of them
-
A condition on the optional side belongs in
ON. PuttingWHERE o.total > 1000after aLEFT JOINremoves theNULLrows and turns it into an inner join. -
One-to-many multiplies rows. Amina appears twice. Joining two one-to-many tables at once multiplies again and breaks
SUMandCOUNT. -
COUNT(*)counts theNULLrow. UseCOUNT(o.id)to count zero orders for Sara. -
NULLkeys never match. Rows with aNULLin the join column are silently dropped from inner joins. -
Index the foreign key. An index on
orders.customer_idlets the database find matches without scanning the whole table.
When to pick which
- INNER JOIN when both sides must exist: an order list showing customer names, where an order without a customer is an error you do not want to display.
- LEFT JOIN for "every X, with its Y if it has one": all customers with their last order, all products with their stock.
-
RIGHT JOIN rarely; rewrite it as a
LEFT JOINso the main table comes first. - FULL OUTER JOIN for comparisons: two imports, two systems, expected versus actual, where you need what is missing on both sides.
-
Anti join (
NOT EXISTS) for "X without Y": customers who never ordered, users who never logged in, invoices with no payment. - CROSS JOIN to generate combinations on purpose, such as every size with every colour, or every day with every store for a report grid.
Originally published at abdeldjalilchougui.biz.
Top comments (0)