MySQL LEFT and RIGHT JOIN
An inner join drops rows without a partner. Outer joins keep them. With LEFT JOIN, every row of the left (first) table appears in the result at least once; where no matching row exists on the right, the right table's columns are NULL.
LEFT JOIN
"Every customer, together with each of their orders":
SELECT c.customer_id, c.first_name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE c.customer_id BETWEEN 5 AND 8
ORDER BY c.customer_id, o.order_id;+-------------+------------+----------+
| customer_id | first_name | order_id |
+-------------+------------+----------+
| 5 | Elena | 5 |
| 5 | Elena | 18 |
| 6 | Farah | 6 |
| 6 | Farah | 16 |
| 7 | George | NULL |
| 8 | Hana | 7 |
| 8 | Hana | 21 |
+-------------+------------+----------+
George (7) has no orders, but he is still in the result, with order_id NULL.
Finding rows without a match
That NULL is useful: a LEFT JOIN followed by WHERE right_table.key IS NULL finds rows that have no partner, an anti-join:
SELECT c.first_name, c.last_name
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
SELECT p.name
FROM products AS p
LEFT JOIN order_details AS d ON d.product_id = p.product_id
WHERE d.order_id IS NULL;+------------+-----------+
| first_name | last_name |
+------------+-----------+
| George | Brown |
| Mia | Jensen |
+------------+-----------+
+----------------------+
| name |
+----------------------+
| Sparkling Water 6x1L |
| Rye Crackers |
+----------------------+
These are the customers who never ordered and the products that were never sold.
ON versus WHERE in an outer join
With an inner join, it doesn't matter whether a condition goes in ON or WHERE. With an outer join it matters a lot:
- a condition in
ONdecides which right rows match; left rows are kept regardless; - a condition in
WHEREfilters the finished result, and removes the NULL rows.
-- shipped orders per customer; customers without any are kept (ON)
SELECT c.first_name, COUNT(o.order_id) AS shipped_orders
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id AND o.status = 'shipped'
WHERE c.customer_id <= 5
GROUP BY c.customer_id, c.first_name
ORDER BY c.customer_id;+------------+----------------+
| first_name | shipped_orders |
+------------+----------------+
| Ana | 0 |
| Ben | 1 |
| Chloe | 0 |
| Daniel | 0 |
| Elena | 1 |
+------------+----------------+
If o.status = 'shipped' were moved into WHERE, only Ben and Elena would remain, because the NULL rows of the other customers fail the test. That accidentally turns the outer join back into an inner join, a very common bug.
RIGHT JOIN
RIGHT JOIN is the mirror image: it keeps every row of the right table. A RIGHT JOIN B gives the same rows as B LEFT JOIN A:
SELECT cat.name AS category, p.name AS product
FROM products AS p
RIGHT JOIN categories AS cat ON cat.category_id = p.category_id AND p.unit_price > 7
ORDER BY cat.name, p.name;+-----------+--------------------+
| category | product |
+-----------+--------------------+
| Bakery | Chocolate Cake |
| Beverages | Espresso Beans 1kg |
| Dairy | Aged Gruyere 250g |
| Household | Laundry Pods (30) |
| Produce | NULL |
| Snacks | NULL |
+-----------+--------------------+
Every category is listed, with its products over 7 or NULL if there are none. Most people find left joins easier to read and rewrite right joins that way, so that the "main" table always comes first.
FULL OUTER JOIN in MySQL
A full outer join keeps unmatched rows from both sides. MySQL doesn't have one, but the UNION of a left join and a right join produces the same result (UNION also removes the duplicate matched rows):
CREATE TABLE stall_a (fruit VARCHAR(10));
CREATE TABLE stall_b (fruit VARCHAR(10));
INSERT INTO stall_a VALUES ('apple'), ('banana'), ('cherry');
INSERT INTO stall_b VALUES ('banana'), ('cherry'), ('damson');
SELECT a.fruit AS at_a, b.fruit AS at_b FROM stall_a a LEFT JOIN stall_b b ON a.fruit = b.fruit
UNION
SELECT a.fruit, b.fruit FROM stall_a a RIGHT JOIN stall_b b ON a.fruit = b.fruit
ORDER BY COALESCE(at_a, at_b);+--------+--------+
| at_a | at_b |
+--------+--------+
| apple | NULL |
| banana | banana |
| cherry | cherry |
| NULL | damson |
+--------+--------+
Test yourself
customers LEFT JOIN orders returns …
- only customers with orders
- every customer, with NULL order columns where there's no order
- every order, with NULL customer columns
- every combination
How do you find products that were never ordered?
- INNER JOIN order_details and WHERE quantity = 0
- LEFT JOIN order_details and WHERE order_details.order_id IS NULL
- RIGHT JOIN products
- CROSS JOIN order_details
How can you get a FULL OUTER JOIN in MySQL?
- FULL JOIN
- OUTER JOIN
- LEFT JOIN ... UNION ... RIGHT JOIN
- It is impossible