SQL Tutorial · Joins and Subqueries · Lesson 19 of 36
SQL INNER JOIN
Combine rows from two tables that match on a key.
Why joins?
Data is split across tables to avoid repetition: an order stores a customer_id, not the customer's name. A join combines tables back together.
INNER JOIN
INNER JOIN (or just JOIN) returns rows that have a match in both tables:
SELECT o.order_id, o.order_date, c.name AS customer
FROM orders AS o
INNER JOIN customers AS c ON c.customer_id = o.customer_id
WHERE o.order_id <= 5
ORDER BY o.order_id;
Output
order_id | order_date | customer 1 | 2026-01-05 | Ada Lovelace 2 | 2026-01-12 | Alan Turing 3 | 2026-02-02 | Ada Lovelace 4 | 2026-02-14 | Grace Hopper 5 | 2026-02-28 | Linus Torvalds
Joining several tables
Chain joins to follow the relationships — orders → items → products:
SELECT o.order_id, p.name AS product, oi.quantity, oi.unit_price,
oi.quantity * oi.unit_price AS line_total
FROM orders o
JOIN order_items oi ON oi.order_id = o.order_id
JOIN products p ON p.product_id = oi.product_id
WHERE o.order_id = 7;
Output
order_id | product | quantity | unit_price | line_total 7 | The Pragmatic Programmer | 1 | 42 | 42 7 | Wireless Mouse | 1 | 24.99 | 24.99 7 | Coffee Mug | 4 | 12 | 48
Always join on keys (ON a.id = b.a_id). Forgetting the ON condition creates a cross join: every row paired with every other row.
Test yourself
Which rows does an INNER JOIN return?
All rows from the left tableOnly rows with a match in both tablesAll rows from both tablesRows without a match
Show answer
Answer: Only rows with a match in both tables