JOIN: combining tables
Combine rows from two or three SQL tables with INNER JOIN and ON, use table aliases, and join then group to calculate spending per customer.
🎯 By the end of this lesson you will be able to:
- Join two tables on a key with JOIN ... ON
- Use table aliases and table.column names
- Join three tables and combine joins with WHERE and GROUP BY
Outline
What you'll learn and why it matters
The orders table says customer 2 bought product 5. People want to read “Ravi Kumar bought a Desk Lamp”. A join puts matching rows from two tables side by side, using the keys from the last lesson. Joins are what turn a database of ids into answers people understand.
Your first join
SELECT orders.order_id, customers.name, orders.order_date
FROM orders
JOIN customers ON orders.customer_id = customers.customer_id
WHERE orders.order_id <= 5;Result: 5 rows
| order_id | name | order_date |
|---|---|---|
| 1 | Asha Patel | 2025-09-01 |
| 2 | Asha Patel | 2025-09-01 |
| 3 | Ravi Kumar | 2025-09-03 |
| 4 | Meera Nair | 2025-09-05 |
| 5 | John Smith | 2025-09-10 |
| Part | Meaning |
|---|---|
FROM orders | Start with the orders table. |
JOIN customers | Bring in the customers table as well. |
ON orders.customer_id = customers.customer_id | Match each order to the customer whose id equals the order's customer_id. |
orders.order_id, customers.name | table.column: say which table each column comes from. |
For each order, the database finds the customer row whose customer_id matches and puts the two rows side by side. Asha appears twice because she placed both order 1 and order 2.
Shorter queries with table aliases
Writing full table names gets long. Give each table a short alias with AS in the FROM and JOIN lines and use it everywhere:
SELECT o.order_id, c.name, c.country, o.status
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id
WHERE c.country = 'India'
ORDER BY o.order_id;Result: 9 rows
| order_id | name | country | status |
|---|---|---|---|
| 1 | Asha Patel | India | delivered |
| 2 | Asha Patel | India | delivered |
| 3 | Ravi Kumar | India | delivered |
| 4 | Meera Nair | India | delivered |
| 6 | Ravi Kumar | India | cancelled |
| 9 | Arjun Singh | India | delivered |
| 10 | Arjun Singh | India | delivered |
| 12 | Asha Patel | India | delivered |
| 15 | Meera Nair | India | shipped |
Notice that WHERE can filter on a column from either table once they are joined.
Joining three tables
Add one JOIN line per extra table. Here each order gets its customer's name and its product's name and price, and we calculate the value of each order line:
SELECT o.order_id,
c.name AS customer,
p.name AS product,
o.quantity,
p.price * o.quantity AS line_total
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id
JOIN products AS p ON o.product_id = p.product_id
WHERE o.order_id <= 6
ORDER BY o.order_id;Result: 6 rows
| order_id | customer | product | quantity | line_total |
|---|---|---|---|---|
| 1 | Asha Patel | Python Basics Book | 1 | 450 |
| 2 | Asha Patel | Notebook A5 | 3 | 360 |
| 3 | Ravi Kumar | Desk Lamp | 1 | 1200 |
| 4 | Meera Nair | Gel Pens (pack of 5) | 2 | 180 |
| 5 | John Smith | SQL Pocket Guide | 1 | 350 |
| 6 | Ravi Kumar | USB-C Cable | 2 | 598 |
Both customers and products have a column called name, so aliasing them as customer and product keeps the result clear.
Join, then group
Joins combine with everything you already know. How much has each customer spent, ignoring cancelled orders?
SELECT c.name, SUM(p.price * o.quantity) AS spent
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id
JOIN products AS p ON o.product_id = p.product_id
WHERE o.status <> 'cancelled'
GROUP BY c.customer_id, c.name
ORDER BY spent DESC, c.name;Result: 8 rows
| name | spent |
|---|---|
| Emma Brown | 1200 |
| Ravi Kumar | 1200 |
| John Smith | 1148 |
| Asha Patel | 1109 |
| Li Wei | 900 |
| Arjun Singh | 690 |
| Meera Nair | 630 |
| Fatima Khan | 399 |
Read it in the order the database runs it: join the three tables, drop cancelled orders, group by customer, add up each group's line totals, then sort. We group by customer_id as well as name because two different customers could share a name.
Practice
Guided exercise
Who bought books?
List every order for a product in the Books category, with the order id, product name and status, in order id order.
- Join
orders AS otoproducts AS pono.product_id = p.product_id. - Filter with
p.category = 'Books'. - Select
o.order_id, p.name, o.statusand sort byo.order_id.
Show the solution
SELECT o.order_id, p.name, o.status
FROM orders AS o
JOIN products AS p ON o.product_id = p.product_id
WHERE p.category = 'Books'
ORDER BY o.order_id;Result: 5 rows
| order_id | name | status |
|---|---|---|
| 1 | Python Basics Book | delivered |
| 5 | SQL Pocket Guide | delivered |
| 8 | Python Basics Book | shipped |
| 14 | SQL Pocket Guide | cancelled |
| 15 | Python Basics Book | shipped |
How to get there: The category lives in products, so you need the join before you can filter on it.
Your turn
Units sold per product
Show each product's name and the total quantity sold (units_sold), ignoring cancelled orders, highest first, then by name.
Show the solution
SELECT p.name, SUM(o.quantity) AS units_sold
FROM orders AS o
JOIN products AS p ON o.product_id = p.product_id
WHERE o.status <> 'cancelled'
GROUP BY p.product_id, p.name
ORDER BY units_sold DESC, p.name;Result: 7 rows
| name | units_sold |
|---|---|
| Notebook A5 | 8 |
| Python Basics Book | 4 |
| Gel Pens (pack of 5) | 3 |
| Water Bottle | 3 |
| Desk Lamp | 2 |
| SQL Pocket Guide | 1 |
| USB-C Cable | 1 |
How to get there: Join to get names, filter out cancelled orders, group by product and add up quantities. The Coffee Mug never sold, so an inner join leaves it out.
Common mistakes
Forgetting the ON condition
You see: A huge result: every order paired with every customer (15 × 10 = 150 rows)
Fix: Always join with ON a.key = b.key.
Joining on the wrong columns, e.g. o.order_id = c.customer_id
You see: Rows appear but pair the wrong people with orders
Fix: Join a foreign key to the primary key it refers to.
Using a column name that exists in both tables without a prefix
You see: ambiguous column name: name
Fix: Prefix it with the table or alias: c.name.
Using an inner join when rows may have no match
You see: Customers or products silently missing
Fix: Use LEFT JOIN (next lesson) when you need every row from one side.
Recap
JOIN other ON a.key = b.keyputs matching rows from two tables side by side.- Inner joins keep only rows with a match in both tables.
- Use aliases (
orders AS o) andalias.columnto keep queries short and clear. - Add a JOIN line per table; then filter, group and sort as usual.
Key terms
- Join
- Combining rows from two tables where a condition matches.
- INNER JOIN
- A join that keeps only rows with a match in both tables. Plain JOIN means INNER JOIN.
- Table alias
- A short name for a table inside a query, such as o for orders.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. What does the ON part of a join say?
2. Customer 7 has no orders. Does she appear in `SELECT c.name FROM customers c JOIN orders o ON o.customer_id = c.customer_id`?
3. You get `ambiguous column name: name`. What is the fix?
4. A join with no ON condition between 15 orders and 10 customers returns how many rows?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.