MySQL Joins and INNER JOIN
Data in a relational database is spread over several tables on purpose: an order stores a customer_id, not the customer's name. A join puts the pieces back together by matching rows from two tables on related columns.
The idea
SELECT order_id, customer_id, order_date FROM orders WHERE order_id <= 3;
SELECT customer_id, first_name, last_name FROM customers WHERE customer_id IN (1, 2, 4);+----------+-------------+------------+
| order_id | customer_id | order_date |
+----------+-------------+------------+
| 1 | 1 | 2025-01-05 |
| 2 | 2 | 2025-01-09 |
| 3 | 4 | 2025-01-18 |
+----------+-------------+------------+
+-------------+------------+-----------+
| customer_id | first_name | last_name |
+-------------+------------+-----------+
| 1 | Ana | Silva |
| 2 | Ben | Carter |
| 4 | Daniel | Weber |
+-------------+------------+-----------+
To see the customer's name next to each order, we pair every order row with the customer row whose customer_id is the same:
SELECT orders.order_id, customers.first_name, customers.last_name, orders.order_date
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id
WHERE orders.order_id <= 3;+----------+------------+-----------+------------+
| order_id | first_name | last_name | order_date |
+----------+------------+-----------+------------+
| 1 | Ana | Silva | 2025-01-05 |
| 2 | Ben | Carter | 2025-01-09 |
| 3 | Daniel | Weber | 2025-01-18 |
+----------+------------+-----------+------------+
The ON clause is the join condition. Here it follows a foreign key, which is by far the most common case.
Kinds of joins
| Join | Returns |
|---|---|
INNER JOIN (or just JOIN) |
only pairs of rows that match the condition |
LEFT JOIN |
every row of the left table, with the matching right rows or NULLs |
RIGHT JOIN |
every row of the right table, with the matching left rows or NULLs |
CROSS JOIN |
every combination of rows (no condition) |
MySQL has no FULL OUTER JOIN; the next lesson shows how to emulate it.
INNER JOIN drops unmatched rows
An inner join keeps only rows that have a partner. Customers who never ordered disappear, which you can check by counting:
SELECT COUNT(*) AS customers FROM customers;
SELECT COUNT(DISTINCT c.customer_id) AS customers_with_orders
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.customer_id;+-----------+
| customers |
+-----------+
| 15 |
+-----------+
+-----------------------+
| customers_with_orders |
+-----------------------+
| 13 |
+-----------------------+
Two of the 15 customers (George and Mia) have no orders, so they don't appear in the joined rows. If you need them, use a LEFT JOIN.
Joining three or more tables
Joins chain: each JOIN ... ON adds another table to the rows built so far. "Which products did Ana Silva buy, and in which category?":
SELECT o.order_id, p.name AS product, cat.name AS category, d.quantity
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.customer_id
JOIN order_details AS d ON d.order_id = o.order_id
JOIN products AS p ON p.product_id = d.product_id
JOIN categories AS cat ON cat.category_id = p.category_id
WHERE c.first_name = 'Ana' AND c.last_name = 'Silva'
ORDER BY o.order_id, p.name;+----------+--------------------+-----------+----------+
| order_id | product | category | quantity |
+----------+--------------------+-----------+----------+
| 1 | Butter Croissant | Bakery | 4 |
| 1 | Espresso Beans 1kg | Beverages | 1 |
| 1 | Whole Milk 1L | Dairy | 2 |
| 4 | Dark Chocolate Bar | Snacks | 5 |
| 4 | Espresso Beans 1kg | Beverages | 2 |
| 14 | Chocolate Cake | Bakery | 1 |
| 14 | Earl Grey Tea | Beverages | 3 |
+----------+--------------------+-----------+----------+
Table aliases (c, o, d, p, cat) keep a query like this readable, and qualifying every column with its table avoids "column is ambiguous" errors, since both products and categories have a name column.
USING
When the two columns have the same name, USING (col) is a short form of ON a.col = b.col:
SELECT order_id, first_name, status
FROM orders
JOIN customers USING (customer_id)
WHERE status = 'processing';+----------+------------+------------+
| order_id | first_name | status |
+----------+------------+------------+
| 20 | Olivia | processing |
| 21 | Hana | processing |
| 22 | Lucia | processing |
+----------+------------+------------+
Note:
FROM a, b WHERE a.id = b.a_idis the old comma-join syntax. It gives the same result as an inner join, but mixing join conditions with filters inWHEREmakes it easy to forget one and accidentally get a cross join. Prefer explicitJOIN ... ON.
Test yourself
Which rows does an INNER JOIN return?
- All rows of both tables
- Only pairs of rows that satisfy the ON condition
- All rows of the left table
- Every combination of rows
Is JOIN on its own the same as INNER JOIN in MySQL?
- Yes
- No, it is a LEFT JOIN
- No, it is a CROSS JOIN
- It is a syntax error
JOIN customers USING (customer_id) is shorthand for …
- ON orders.customer_id = customers.customer_id
- WHERE customer_id IS NOT NULL
- a CROSS JOIN
- ON orders.order_id = customers.customer_id