TutreNews Learn

Combining tables

INNER JOIN

Goal: Combine related rows from two or more tables · 10 min

Data is split into tables to avoid repetition: an order only stores customer_id, not the customer's name and city. A foreign key like orders.customer_id points to the primary key customers.id.

A JOIN puts the pieces back together:

SELECT o.id, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id;

o and c are table aliases. The ON condition says which rows belong together. An INNER JOIN (plain JOIN) keeps only rows that have a match in both tables. You can chain several joins to walk from orders to order_items to products.

Worked example: What did Ada buy?

  1. Join orders → order_items → products and customers.
  2. Filter by the customer name.
  3. Ada: Laptop, 2 × Mouse, Headphones.
SELECT p.name, oi.qty
FROM orders o
JOIN customers c 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 c.name = 'Ada'
ORDER BY p.name;

Output:

Headphones | 1
Laptop | 1
Mouse | 2