11. JOIN: reading several tables at once
Put tables linked by their id numbers together for one answer: orders with the customer's name, lines with the product's name.
Why the data is split into tables
Look at product again. A product doesn't keep its category's name, only the number category_id:
SELECT id, name, category_id FROM product WHERE id <= 4;| id | name | category_id |
|---|---|---|
| 1 | Coffee | 1 |
| 2 | Green Tea | 1 |
| 3 | Orange Juice | 1 |
| 4 | Croissant | 2 |
Why not just write Drinks there? Because the day the shop renames Drinks to Beverages, every product in it would have to be changed, and miss one and the data contradicts itself. With the name kept in one place, the category table, one row changes and that's it. Orders are the same: they keep only customer_id, not the customer's name, city and phone again on every order.
Splitting keeps the data right and free of repeats. But when we read, we want names, not numbers, so the tables have to be joined back together: JOIN.
INNER JOIN ... ON
Put the first table after FROM, the second after JOIN, and say after ON when a row of one goes with a row of the other:
SELECT product.name, category.name
FROM product
JOIN category ON product.category_id = category.id
ORDER BY product.id;| name | name |
|---|---|
| Coffee | Drinks |
| Green Tea | Drinks |
| Orange Juice | Drinks |
| Croissant | Bakery |
| Banana Bread | Bakery |
| Cheesecake | Bakery |
| Potato Chips | Snacks |
| Dried Mango | Snacks |
| Cashew Nuts | Snacks |
| Notebook | Stationery |
| Pen | Stationery |
| Highlighter | Stationery |
The database pairs every product with the category whose id matches its category_id: 12 rows, one per product.
Both tables have a column called name, so table.column says which one is meant. The result also has two columns headed name; give them names of their own with AS (as in the next example).
JOIN alone and INNER JOIN are the same thing. Inner means only rows that found a partner: a row with none is left out of the result (chapter 12 is about those).
Short names for tables
Long table names written again and again get tiring. Give a table a short name (an alias) after its name, with or without AS:
SELECT o.id, o.ordered, c.name AS customer, o.status
FROM orders AS o
JOIN customer AS c ON c.id = o.customer_id
ORDER BY o.id;| id | ordered | customer | status |
|---|---|---|---|
| 1 | 2026-09-02 | Somchai Jaidee | paid |
| 2 | 2026-09-05 | Malee Srisuk | shipped |
| 3 | 2026-09-12 | Somchai Jaidee | shipped |
| 4 | 2026-09-20 | Anan Wongsa | paid |
| 5 | 2026-10-01 | Pim Chaiya | cancelled |
| 6 | 2026-10-03 | Narin Thongdee | shipped |
| 7 | 2026-10-10 | Malee Srisuk | paid |
| 8 | 2026-10-15 | Kanya Boonmee | shipped |
| 9 | 2026-10-22 | Wichai Saelim | paid |
| 10 | 2026-10-28 | Somchai Jaidee | paid |
| 11 | 2026-11-02 | Siriporn Kaewdee | shipped |
| 12 | 2026-11-05 | Pim Chaiya | paid |
| 13 | 2026-11-11 | Kanya Boonmee | paid |
| 14 | 2026-11-18 | Anan Wongsa | cancelled |
| 15 | 2026-11-25 | Wichai Saelim | paid |
All 15 orders with the customer's name. o is orders and c is customer; once given, the short name is used all through the statement.
JOIN works with everything so far: WHERE, ORDER BY, GROUP BY. The orders of Bangkok customers that haven't been shipped yet:
SELECT o.id, c.name, c.city, o.status
FROM orders o
JOIN customer c ON c.id = o.customer_id
WHERE c.city = 'Bangkok' AND o.status = 'paid'
ORDER BY o.id;| id | name | city | status |
|---|---|---|---|
| 1 | Somchai Jaidee | Bangkok | paid |
| 4 | Anan Wongsa | Bangkok | paid |
| 9 | Wichai Saelim | Bangkok | paid |
| 10 | Somchai Jaidee | Bangkok | paid |
| 15 | Wichai Saelim | Bangkok | paid |
5 orders, from 3 Bangkok customers. WHERE can use a column of any table joined in.
An order's lines: product names and line totals
order_item holds only numbers. Join it to product for the product's name, and work out each line's total:
SELECT i.order_id, p.name AS product, i.qty, i.price, i.qty * i.price AS line_total
FROM order_item i
JOIN product p ON p.id = i.product_id
WHERE i.order_id = 9
ORDER BY p.name;| order_id | product | qty | price | line_total |
|---|---|---|---|---|
| 9 | Cheesecake | 2 | 120.0 | 240.0 |
| 9 | Coffee | 2 | 55.0 | 110.0 |
| 9 | Croissant | 4 | 35.0 | 140.0 |
Order 9 has 3 lines, adding up to 490 baht, as chapter 9 found.
order_item keeps its own price (i.price): the price when it sold. p.price is the price today. When the shop raises a price one day, old orders are still right; so sales are always worked out from i.price.
Three tables and more
JOINs follow one another, each with its own ON. Every order line with its date, customer, product and category joins five tables:
SELECT o.id AS order_id, o.ordered, c.name AS customer, p.name AS product, cat.name AS category, i.qty
FROM order_item i
JOIN orders o ON o.id = i.order_id
JOIN customer c ON c.id = o.customer_id
JOIN product p ON p.id = i.product_id
JOIN category cat ON cat.id = p.category_id
ORDER BY o.id, p.name;| order_id | ordered | customer | product | category | qty |
|---|---|---|---|---|---|
| 1 | 2026-09-02 | Somchai Jaidee | Coffee | Drinks | 2 |
| 1 | 2026-09-02 | Somchai Jaidee | Croissant | Bakery | 2 |
| 2 | 2026-09-05 | Malee Srisuk | Dried Mango | Snacks | 1 |
| 2 | 2026-09-05 | Malee Srisuk | Potato Chips | Snacks | 3 |
| 3 | 2026-09-12 | Somchai Jaidee | Cheesecake | Bakery | 1 |
| 3 | 2026-09-12 | Somchai Jaidee | Coffee | Drinks | 1 |
| 4 | 2026-09-20 | Anan Wongsa | Notebook | Stationery | 5 |
| 4 | 2026-09-20 | Anan Wongsa | Pen | Stationery | 10 |
| 5 | 2026-10-01 | Pim Chaiya | Cashew Nuts | Snacks | 1 |
| 6 | 2026-10-03 | Narin Thongdee | Banana Bread | Bakery | 1 |
| 6 | 2026-10-03 | Narin Thongdee | Green Tea | Drinks | 4 |
| 6 | 2026-10-03 | Narin Thongdee | Potato Chips | Snacks | 2 |
| 7 | 2026-10-10 | Malee Srisuk | Coffee | Drinks | 1 |
| 7 | 2026-10-10 | Malee Srisuk | Dried Mango | Snacks | 2 |
| 8 | 2026-10-15 | Kanya Boonmee | Notebook | Stationery | 3 |
| 8 | 2026-10-15 | Kanya Boonmee | Pen | Stationery | 20 |
| 9 | 2026-10-22 | Wichai Saelim | Cheesecake | Bakery | 2 |
| 9 | 2026-10-22 | Wichai Saelim | Coffee | Drinks | 2 |
| 9 | 2026-10-22 | Wichai Saelim | Croissant | Bakery | 4 |
| 10 | 2026-10-28 | Somchai Jaidee | Green Tea | Drinks | 1 |
28 rows, as many as order_item has: every line finds exactly one order, customer, product and category.
Read a long JOIN as a path: from the line (order_item) to its order (orders), from the order to its customer (customer), and from the line to its product (product) and on to its category (category).
A join can be grouped, too. What each customer spent, cancelled orders left out:
SELECT c.name AS customer, count(DISTINCT o.id) AS orders, sum(i.qty * i.price) AS spent
FROM customer c
JOIN orders o ON o.customer_id = c.id
JOIN order_item i ON i.order_id = o.id
WHERE o.status <> 'cancelled'
GROUP BY c.id, c.name
ORDER BY spent DESC, c.name;| customer | orders | spent |
|---|---|---|
| Wichai Saelim | 2 | 865.0 |
| Kanya Boonmee | 2 | 480.0 |
| Malee Srisuk | 2 | 400.0 |
| Somchai Jaidee | 3 | 400.0 |
| Anan Wongsa | 1 | 300.0 |
| Siriporn Kaewdee | 1 | 300.0 |
| Narin Thongdee | 1 | 290.0 |
| Pim Chaiya | 1 | 175.0 |
Wichai Saelim spent the most, 865 baht. There are 8 customers in the result: the 2 who never ordered are missing, because the JOIN found no order to pair them with. Chapter 12 brings them back.
count(DISTINCT o.id) matters here: joined to order_item, one order becomes several rows (one per line), and count(*) would count lines, not orders.
USING when the names match
When the matching columns have the same name in both tables, USING (name) can stand in for ON. Our shop names them differently (orders.id and order_item.order_id), so it doesn't fit there. Make a small promotion table with a product_id column, like order_item's (making tables is in chapters 16 and 20; just run it for now):
CREATE TABLE promo (product_id INTEGER, discount INTEGER);
INSERT INTO promo VALUES (1, 10), (7, 5), (11, 2);SELECT order_id, product_id, qty, discount
FROM order_item
JOIN promo USING (product_id)
ORDER BY order_id, product_id;| order_id | product_id | qty | discount |
|---|---|---|---|
| 1 | 1 | 2 | 10 |
| 2 | 7 | 3 | 5 |
| 3 | 1 | 1 | 10 |
| 4 | 11 | 10 | 2 |
| 6 | 7 | 2 | 5 |
| 7 | 1 | 1 | 10 |
| 8 | 11 | 20 | 2 |
| 9 | 1 | 2 | 10 |
| 11 | 7 | 6 | 5 |
| 12 | 1 | 1 | 10 |
| 15 | 11 | 5 | 2 |
11 lines: every line that sold product 1, 7 or 11, the ones on promotion.
USING (product_id) is ON order_item.product_id = promo.product_id, and product_id can be written alone, with no table, since both tables have the same value there.
Error: ambiguous column name
Write a column that both tables have, without saying whose, and the database can't choose:
SELECT id, name FROM product p JOIN category c ON p.category_id = c.id;Ambiguous means it could be either: id is in product and in category. The fix is the table's name (or short name) before every column whose name is shared:
SELECT p.id, p.name, c.name AS category FROM product p JOIN category c ON p.category_id = c.id ORDER BY p.id;| id | name | category |
|---|---|---|
| 1 | Coffee | Drinks |
| 2 | Green Tea | Drinks |
| 3 | Orange Juice | Drinks |
| 4 | Croissant | Bakery |
| 5 | Banana Bread | Bakery |
| 6 | Cheesecake | Bakery |
| 7 | Potato Chips | Snacks |
| 8 | Dried Mango | Snacks |
| 9 | Cashew Nuts | Snacks |
| 10 | Notebook | Stationery |
| 11 | Pen | Stationery |
| 12 | Highlighter | Stationery |
In a statement with a JOIN, put the short name before every column, even one whose name isn't shared. The reader sees at once which table each comes from, and the day someone adds a column of the same name to the other table, the statement doesn't break.
Don't forget ON. SQLite lets you write JOIN without it, and then pairs every row with every row: 12 products and 4 categories make 48 rows, though each product is in only one category. When a result has far more rows than you expected, check the ON first.
SELECT count(*) FROM product JOIN category;| count(*) |
|---|
| 48 |
- Show the name, price and category name of the products over 50 baht, by price.
- Show every line of order 6: the product's name, the quantity and the line total.
- Which orders did customers from
Chiang Maiplace? Show the order number, the date and the customer's name. - Sales per category (by name), shipped orders (
shipped) only.
Show the answer
SELECT p.name, p.price, c.name AS category FROM product p JOIN category c ON c.id = p.category_id WHERE p.price > 50 ORDER BY p.price;
SELECT p.name, i.qty, i.qty * i.price AS line_total FROM order_item i JOIN product p ON p.id = i.product_id WHERE i.order_id = 6 ORDER BY p.name;
SELECT o.id, o.ordered, c.name FROM orders o JOIN customer c ON c.id = o.customer_id WHERE c.city = 'Chiang Mai' ORDER BY o.id;
SELECT cat.name, sum(i.qty * i.price) AS sales FROM order_item i JOIN orders o ON o.id = i.order_id JOIN product p ON p.id = i.product_id JOIN category cat ON cat.id = p.category_id WHERE o.status = 'shipped' GROUP BY cat.name ORDER BY sales DESC;| name | price | category |
|---|---|---|
| Coffee | 55.0 | Drinks |
| Banana Bread | 60.0 | Bakery |
| Dried Mango | 90.0 | Snacks |
| Cheesecake | 120.0 | Bakery |
| Cashew Nuts | 150.0 | Snacks |
| name | qty | line_total |
|---|---|---|
| Banana Bread | 1 | 60.0 |
| Green Tea | 4 | 180.0 |
| Potato Chips | 2 | 50.0 |
| id | ordered | name |
|---|---|---|
| 2 | 2026-09-05 | Malee Srisuk |
| 7 | 2026-10-10 | Malee Srisuk |
| 8 | 2026-10-15 | Kanya Boonmee |
| 13 | 2026-11-11 | Kanya Boonmee |
| name | sales |
|---|---|
| Snacks | 515.0 |
| Stationery | 390.0 |
| Drinks | 235.0 |
| Bakery | 180.0 |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made