Joins Lesson 14 of 21
SQL JOIN — Combining Tables with INNER JOIN
Combine rows from two or more tables with INNER JOIN and ON — show customer names on orders, product names on order items, and compute sales.
The orders table stores customer_id, not the customer's name. To show "Order 3 – Maria Santos", you need data from two tables at once. That's what a JOIN does: it matches rows from different tables using a shared value, usually an id.
INNER JOIN
SELECT orders.id, orders.order_date, customers.name, orders.status
FROM orders
INNER JOIN customers ON orders.customer_id = customers.id;
How to read it:
- Start with the
orderstable. INNER JOIN customers: bring in the customers table.ON orders.customer_id = customers.id: match each order to the customer whoseidequals the order'scustomer_id.
Because both tables have columns named id and name, we write table.column to say which one we mean.
Shorter with aliases
SELECT o.id, o.order_date, c.name, o.status
FROM orders o
JOIN customers c ON o.customer_id = c.id;
Writing just JOIN means INNER JOIN. Aliases like o and c make joins much easier to read and type.
What "inner" means
An inner join returns only rows that have a match in both tables. Customers who never ordered (Ana Cruz has one, but Carlo Mendoza has none) don't appear in the result above, because there's no order to match them with.
Joining three tables
order_items links orders to products. To see product names in each order, join it with products:
SELECT oi.order_id, p.name, oi.qty, p.price, oi.qty * p.price AS line_total
FROM order_items oi
JOIN products p ON oi.product_id = p.id
ORDER BY oi.order_id;
Add the customers too, by chaining another JOIN:
SELECT c.name AS customer, o.order_date, p.name AS product, oi.qty
FROM order_items oi
JOIN orders o ON oi.order_id = o.id
JOIN customers c ON o.customer_id = c.id
JOIN products p ON oi.product_id = p.id
ORDER BY o.order_date, c.name;
JOIN + GROUP BY: real reports
SELECT c.name, SUM(oi.qty * p.price) AS total_spent
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
GROUP BY c.name
ORDER BY total_spent DESC;
SELECT c.name, c.phone, SUM(oi.qty * p.price) AS utang
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
WHERE o.status = 'utang'
GROUP BY c.id
ORDER BY utang DESC;
This is a real, useful report for Aling Nena: who owes money, how much, and their phone number to send a reminder.
Students with their grades
SELECT s.first_name || ' ' || s.last_name AS student, g.subject, g.score
FROM students s
JOIN grades g ON g.student_id = s.id
WHERE s.grade = 10
ORDER BY student, g.subject;
Ambiguous column names
If both tables have a column with the same name and you don't say which one, you get an error like "ambiguous column name: id". Always prefix shared names: o.id, c.id.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04