SQL Joins · Lesson 13 of 30
SQL INNER JOIN
A well-designed database splits information into separate tables – customers in one, orders in another – to avoid repeating data. But questions usually need both: "show each order with the customer's name". A join puts related rows back together. INNER JOIN, the most common kind, returns only rows that have a match in both tables.
In this lesson
- How INNER JOIN matches rows
- The ON condition and table aliases
- Joining and aggregating together
- USING, ambiguous columns and accidental duplicates
Your first join
SELECT o.order_id, o.order_date, c.name AS customer, o.status
FROM orders AS o
INNER JOIN customers AS c ON c.customer_id = o.customer_id
WHERE o.order_id <= 105
ORDER BY o.order_id;
+----------+------------+------------+-----------+ | order_id | order_date | customer | status | +----------+------------+------------+-----------+ | 101 | 2025-08-02 | Asha Verma | delivered | | 102 | 2025-08-05 | Ravi Kumar | delivered | | 103 | 2025-08-19 | Asha Verma | delivered | | 104 | 2025-09-01 | Meera Nair | cancelled | | 105 | 2025-09-07 | Kiran Rao | delivered | +----------+------------+------------+-----------+
How the join works
- Take a row from ordersorder 101, customer_id = 1
- Find customers rows where ON is truecustomers.customer_id = 1 → Asha Verma
- Output the combined rowcolumns from both tables
- No match?the row is left out (that's the "inner")
JOINon its own meansINNER JOIN.ONstates how rows relate – almost always foreign key = primary key.- Aliases (
o,c) keep queries short; prefix every column with its alias so readers know where it comes from.
INNER JOIN keeps only matches
SELECT COUNT(*) AS customers FROM customers;
SELECT COUNT(DISTINCT c.customer_id) AS customers_with_orders
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id;
+-----------+ | customers | +-----------+ | 8 | +-----------+ +-----------------------+ | customers_with_orders | +-----------------------+ | 7 | +-----------------------+
Vikram (customer 8) has no orders, so the inner join drops him. Products without a category disappear the same way:
SELECT p.name AS product, c.name AS category
FROM products p
JOIN categories c ON c.category_id = p.category_id
ORDER BY c.name, p.name;
+---------------------+-------------+ | product | category | +---------------------+-------------+ | Clean Code | Books | | JavaScript Guide | Books | | SQL in 10 Minutes | Books | | Laptop Stand | Electronics | | Mechanical Keyboard | Electronics | | USB-C Hub | Electronics | | Wireless Mouse | Electronics | | A5 Notebook | Stationery | | Gel Pen Pack | Stationery | +---------------------+-------------+
Desk Lamp (no category) and Fitness (no products) are both missing. When you need the unmatched rows too, use an outer join – the next lesson.
Join and aggregate: totals per customer
SELECT c.name,
COUNT(*) AS orders,
MIN(o.order_date) AS first_order
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name
ORDER BY orders DESC, c.name;
+-------------+--------+-------------+ | name | orders | first_order | +-------------+--------+-------------+ | Asha Verma | 3 | 2025-08-02 | | Kiran Rao | 2 | 2025-09-07 | | Meera Nair | 2 | 2025-09-01 | | Ravi Kumar | 2 | 2025-08-05 | | Arjun Singh | 1 | 2025-10-10 | | Neha Gupta | 1 | 2025-11-02 | | Zoya Khan | 1 | 2025-09-15 | +-------------+--------+-------------+
Group by the primary key (c.customer_id) as well as the name – two different customers could share a name.
Revenue per category: three tables
SELECT cat.name AS category,
SUM(i.quantity) AS units,
SUM(i.quantity * i.unit_price) AS revenue
FROM order_items i
JOIN products p ON p.product_id = i.product_id
JOIN categories cat ON cat.category_id = p.category_id
GROUP BY cat.category_id, cat.name
ORDER BY revenue DESC;
+-------------+-------+----------+ | category | units | revenue | +-------------+-------+----------+ | Electronics | 9 | 11691.00 | | Books | 7 | 3050.00 | | Stationery | 12 | 1230.00 | +-------------+-------+----------+
Each JOIN adds one more table, linked by its own ON condition. The Desk Lamp sale (₹899) isn't in any category, so it's not in these totals – a subtle way joins can hide data in reports.
USING and ambiguous columns
-- When both columns have the same name, USING is a shortcut for ON a.col = b.col
SELECT order_id, name, status
FROM orders
JOIN customers USING (customer_id)
WHERE status = 'pending';
+----------+-------------+---------+ | order_id | name | status | +----------+-------------+---------+ | 108 | Arjun Singh | pending | | 111 | Kiran Rao | pending | +----------+-------------+---------+
Common mistakes
- Column 'name' in field list is ambiguous – both tables have a
namecolumn; prefix it:c.name. - Forgetting the ON condition (or using the old comma syntax without WHERE) multiplies every row by every row – a cross join that can return millions of rows.
- Joining one-to-many tables then using SUM on the "one" side counts it several times. Aggregate at the right level (see chapter 6 for pre-aggregating in a subquery).
Practice
Try it yourself
List the products in order 105 with their quantities and line totals.
Show solution
SELECT p.name, i.quantity, i.unit_price, i.quantity * i.unit_price AS line_total FROM order_items i JOIN products p ON p.product_id = i.product_id WHERE i.order_id = 105;+---------------------+----------+------------+------------+ | name | quantity | unit_price | line_total | +---------------------+----------+------------+------------+ | Wireless Mouse | 2 | 599.00 | 1198.00 | | Mechanical Keyboard | 1 | 2499.00 | 2499.00 | +---------------------+----------+------------+------------+
Show how much each customer has spent on delivered orders, highest first.
Show solution
SELECT c.name, SUM(i.quantity * i.unit_price) AS spent FROM customers c JOIN orders o ON o.customer_id = c.customer_id JOIN order_items i ON i.order_id = o.order_id WHERE o.status = 'delivered' GROUP BY c.customer_id, c.name ORDER BY spent DESC;+------------+---------+ | name | spent | +------------+---------+ | Ravi Kumar | 3798.00 | | Kiran Rao | 3697.00 | | Asha Verma | 1639.00 | | Meera Nair | 1000.00 | | Neha Gupta | 700.00 | +------------+---------+
Quick check
What happens to a row with no match in an INNER JOIN?
It's excluded from the result.
Is JOIN the same as INNER JOIN?
Yes.
Why prefix columns with table aliases?
To avoid ambiguous-column errors and make clear which table each column comes from.
Summary
- INNER JOIN combines rows where the ON condition matches; unmatched rows are dropped.
- Join on foreign key = primary key; use aliases and prefix every column.
- Chain JOINs for more tables, and watch for rows silently lost or double-counted.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.