INNER JOIN — The Most Common Join
Combine two tables and return only the rows that have a match in both — the workhorse of relational databases.
INNER JOIN is the handshake between two tables. Only rows that match on both sides make it through.
EXERCISE
1What is a JOIN and Why Do We Need It?
Data in a relational database is split across tables on purpose. JOINs let you bring it back together when you need it — without duplicating storage.
The problem JOINs solve:
Your customer data is in customers. Their orders are in orders. When you want to see "customer name + their order total", you need to combine both tables.
customers: orders:
┌────┬──────────┐ ┌────┬─────────────┬────────┐
│ id │ name │ │ id │ customer_id │ total │
├────┼──────────┤ ├────┼─────────────┼────────┤
│ 1 │ Arjun │ │ 1 │ 1 │ 2999 │
│ 2 │ Priya │ │ 2 │ 1 │ 499 │
│ 3 │ Rohan │ │ 3 │ 2 │ 1200 │
└────┴──────────┘ └────┴─────────────┴────────┘
INNER JOIN — match on a shared key:
SELECT
c.name AS customer_name,
o.id AS order_id,
o.total AS order_total
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id;
-- Result:
-- customer_name | order_id | order_total
-- Arjun | 1 | 2999
-- Arjun | 2 | 499
-- Priya | 3 | 1200
-- (Rohan not returned — he has no orders)
Table aliases make JOIN queries readable:
-- Without aliases (verbose):
FROM customers INNER JOIN orders ON customers.id = orders.customer_id
-- With aliases (cleaner):
FROM customers c INNER JOIN orders o ON c.id = o.customer_id
Pro Tip: INNER JOIN only returns rows that have a match in BOTH tables. Rohan is missing from the result because he has no orders. If you need all customers including those with no orders, use LEFT JOIN (next lesson).
EXERCISE
2INNER JOIN with WHERE and Aggregation
JOIN is most powerful when combined with WHERE to filter and GROUP BY to summarise — letting you answer joined questions like "which customers spent the most?"
JOIN + WHERE:
-- Orders placed in January with customer names:
SELECT
c.name,
o.id AS order_id,
o.total,
o.order_date
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id
WHERE MONTH(o.order_date) = 1
AND o.status = 'completed'
ORDER BY o.total DESC;
JOIN + GROUP BY — aggregating across tables:
-- Total spent per customer:
SELECT
c.name,
COUNT(o.id) AS order_count,
SUM(o.total) AS total_spent
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id
GROUP BY c.id, c.name
ORDER BY total_spent DESC;
Three-table JOIN:
-- Order details with customer name and product name:
SELECT
c.name AS customer,
p.name AS product,
oi.quantity,
oi.unit_price
FROM order_items oi
INNER JOIN orders o ON oi.order_id = o.id
INNER JOIN customers c ON o.customer_id = c.id
INNER JOIN products p ON oi.product_id = p.id
WHERE o.status = 'completed';
Pro Tip: When joining, always qualify column names with the table alias:
c.name, not justname. When two tables both have anamecolumn, an unqualifiednameis ambiguous and will error.
EXERCISE
3JOIN Conditions and Filtering Joins
The ON clause defines how tables connect. You can add extra conditions to ON, or filter after joining with WHERE — and knowing the difference matters.
ON vs WHERE for filtering:
-- Filter in WHERE (most common pattern):
SELECT c.name, o.total
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id
WHERE o.status = 'completed'; -- filter AFTER join
-- Filter in ON (less common, same result for INNER JOIN):
SELECT c.name, o.total
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id
AND o.status = 'completed'; -- filter AS PART OF join condition
For INNER JOIN, both give the same result. The difference matters for LEFT JOIN (covered next lesson).
Non-equi JOIN (joining on a range instead of equality):
-- Match customers to their membership tier based on total spent:
SELECT
c.name,
c.total_spent,
t.tier_name
FROM customers c
INNER JOIN membership_tiers t
ON c.total_spent BETWEEN t.min_spend AND t.max_spend;
USING clause (shorthand when column names match):
-- Standard ON:
FROM customers c INNER JOIN orders o ON c.id = o.customer_id
-- If both tables had a column called 'customer_id':
FROM customers c INNER JOIN orders o USING (customer_id)
-- Cleaner, but only works when the column name is identical in both tables
Pro Tip: For INNER JOIN, filtering in ON or WHERE gives the same result. For LEFT JOIN, it is completely different — we cover this in the next lesson, and it is one of the most common interview questions.
