Nishaglobal Education logo

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.

Lesson 9 of 1235 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. Your first join
  3. Shorter queries with table aliases
  4. Joining three tables
  5. Join, then group
  6. Practice
  7. Common mistakes
  8. Recap
  9. Quiz
  10. Mark lesson complete

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

SQL
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

Query result
order_idnameorder_date
1Asha Patel2025-09-01
2Asha Patel2025-09-01
3Ravi Kumar2025-09-03
4Meera Nair2025-09-05
5John Smith2025-09-10
PartMeaning
FROM ordersStart with the orders table.
JOIN customersBring in the customers table as well.
ON orders.customer_id = customers.customer_idMatch each order to the customer whose id equals the order's customer_id.
orders.order_id, customers.nametable.column: say which table each column comes from.
ordersorder_idcustomer_id113243customerscustomer_idname1Asha2Ravi3Meera7SaraSara: no matching orderJOIN ... ON customer_idresult: one row per matchorder_idcustomer_idname11Asha32Ravi43MeeraSara is not in the result: inner joins keep matches only
Before: two small tables. After the JOIN: one row for every matching pair. Each order is matched to its customer through customer_id; a customer with no orders does not appear.

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:

SQL
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

Query result
order_idnamecountrystatus
1Asha PatelIndiadelivered
2Asha PatelIndiadelivered
3Ravi KumarIndiadelivered
4Meera NairIndiadelivered
6Ravi KumarIndiacancelled
9Arjun SinghIndiadelivered
10Arjun SinghIndiadelivered
12Asha PatelIndiadelivered
15Meera NairIndiashipped

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:

SQL
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

Query result
order_idcustomerproductquantityline_total
1Asha PatelPython Basics Book1450
2Asha PatelNotebook A53360
3Ravi KumarDesk Lamp11200
4Meera NairGel Pens (pack of 5)2180
5John SmithSQL Pocket Guide1350
6Ravi KumarUSB-C Cable2598

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?

SQL
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

Query result
namespent
Emma Brown1200
Ravi Kumar1200
John Smith1148
Asha Patel1109
Li Wei900
Arjun Singh690
Meera Nair630
Fatima Khan399

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.

  1. Join orders AS o to products AS p on o.product_id = p.product_id.
  2. Filter with p.category = 'Books'.
  3. Select o.order_id, p.name, o.status and sort by o.order_id.
Show the solution
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

Query result
order_idnamestatus
1Python Basics Bookdelivered
5SQL Pocket Guidedelivered
8Python Basics Bookshipped
14SQL Pocket Guidecancelled
15Python Basics Bookshipped

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
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

Query result
nameunits_sold
Notebook A58
Python Basics Book4
Gel Pens (pack of 5)3
Water Bottle3
Desk Lamp2
SQL Pocket Guide1
USB-C Cable1

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.key puts matching rows from two tables side by side.
  • Inner joins keep only rows with a match in both tables.
  • Use aliases (orders AS o) and alias.column to 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. 1. What does the ON part of a join say?

  2. 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. 3. You get `ambiguous column name: name`. What is the fix?

  4. 4. A join with no ON condition between 15 orders and 10 customers returns how many rows?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.