Inner joins

Topic 1 of 4 in Joining tables12 min readBeginnerFree course

Overview

The Bean & Leaf data is spread over six tables on purpose — each fact is stored once (you saw why in How relational databases work). But questions cut across tables: which customer placed this order? What’s the name of the product on this line? How much revenue did each category bring in? A join puts the pieces back together. It’s the single most important skill in SQL, and this chapter is about nothing else.

Your first join

The orders table knows each order’s customer_id; the customers table knows each customer’s name. Join them on that key:

SQL
SELECT orders.id, orders.ordered_at::date AS day, customers.first_name
FROM orders
JOIN customers ON customers.id = orders.customer_id
WHERE orders.id <= 5
ORDER BY orders.id;
Text
 id |    day     | first_name 
----+------------+------------
  1 | 2026-01-05 | Asha
  2 | 2026-01-12 | Ben
  3 | 2026-01-28 | Asha
  4 | 2026-02-03 | Chen
  5 | 2026-02-14 | Dee
(5 rows)

Reading it:

  • FROM orders JOIN customers — combine the two tables.
  • ON customers.id = orders.customer_id — a row of orders belongs with the row of customers whose id equals the order’s customer_id. This is the join condition.
  • orders.id, customers.first_name — because two tables are involved, you say which table each column comes from.

INNER JOIN: matching rows. customers has rows 1 Asha, 2 Ben and 9 Isha; orders has rows 1 (customer 1), 2 (customer 2) and 3 (customer 1). Lines connect each order to its customer: Asha to orders 1 and 3, Ben to order 2. Isha has no orders, so she is dropped. The result is Asha 1, Ben 2, Asha 3. The query is SELECT c.first_name, o.id FROM customers AS c JOIN orders AS o ON o.customer_id = c.id. JOIN … ON pairs every row on the left with every row on the right that satisfies the condition. A customer with two orders appears twice; a customer with none disappears. JOIN means INNER JOIN.

Conceptually, the database pairs every row of one table with every row of the other and keeps only the pairs where the ON condition is true. (It doesn’t really do it that way — it uses much faster methods — but the result is the same.)

Table aliases

Writing customers.first_name everywhere gets long. Give each table a short alias in FROM and use it instead:

SQL
SELECT o.id, o.ordered_at::date AS day, c.first_name, c.city
FROM orders AS o
JOIN customers AS c ON c.id = o.customer_id
WHERE o.id <= 5
ORDER BY o.id;
Text
 id |    day     | first_name |   city    
----+------------+------------+-----------
  1 | 2026-01-05 | Asha       | Pune
  2 | 2026-01-12 | Ben        | Leeds
  3 | 2026-01-28 | Asha       | Pune
  4 | 2026-02-03 | Chen       | Singapore
  5 | 2026-02-14 | Dee        | Lagos
(5 rows)

AS is optional for table aliases (FROM orders o is very common). Once you’ve given a table an alias, use the alias everywhere in the query.

Qualifying columns

If a column name exists in only one of the tables, you can leave off the prefix. But if it exists in both, you must say which you mean:

SQL
SELECT id, first_name, status
FROM orders AS o
JOIN customers AS c ON c.id = o.customer_id;
Text
ERROR:  column reference "id" is ambiguous
LINE 1: SELECT id, first_name, status
               ^

Both tables have an id. The habit professionals use: qualify every column in a join, even the unambiguous ones. It makes the query readable without having to remember every table’s columns, and it won’t break when someone later adds a column with a clashing name.

Inner means “matching only”

JOIN is short for INNER JOIN: only rows that find a partner appear in the result. Two consequences:

Rows without a match disappear. Isha and Jack have never ordered, so they’re nowhere in a customers–orders join:

SQL
SELECT DISTINCT c.first_name
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
ORDER BY c.first_name;
Text
 first_name 
------------
 Asha
 Ben
 Chen
 Dee
 Elena
 Farhan
 Grace
 Hugo
(8 rows)

Eight names, not ten. Usually that’s what you want; when it isn’t, you need an outer join (next lesson).

Rows with several matches multiply. Asha has five orders, so she appears five times:

SQL
SELECT c.first_name, o.id AS order_id, o.status
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE c.id = 1
ORDER BY o.id;
Text
 first_name | order_id |  status   
------------+----------+-----------
 Asha       |        1 | delivered
 Asha       |        3 | delivered
 Asha       |        9 | delivered
 Asha       |       17 | delivered
 Asha       |       23 | pending
(5 rows)

This is the heart of joins: the result has one row per matching pair. When a join returns more rows than you expected, it’s almost always because one row matched several.

Joining products to their lines

The order_items table stores product ids. Join to products to see names:

SQL
SELECT oi.order_id, p.name, oi.quantity, oi.unit_price,
       oi.quantity * oi.unit_price AS line_total
FROM order_items AS oi
JOIN products AS p ON p.id = oi.product_id
WHERE oi.order_id IN (1, 2)
ORDER BY oi.order_id, p.name;
Text
 order_id |         name          | quantity | unit_price | line_total 
----------+-----------------------+----------+------------+------------
        1 | Assam Breakfast       |        1 |       9.50 |       9.50
        1 | Ethiopian Yirgacheffe |        2 |      14.50 |      29.00
        2 | Burr Grinder          |        1 |      89.00 |      89.00
        2 | Colombian Supremo     |        2 |      12.00 |      24.00
(4 rows)

Every join in this database follows the same pattern: foreign key = primary key. The schema diagram in chapter 1 is your map of which columns to connect.

Joins with WHERE, GROUP BY and ORDER BY

A join builds a combined set of rows; everything you’ve learned then works on it as if it were one table. Revenue per category name — joining order_items to products to categories:

SQL
SELECT cat.name AS category,
       sum(oi.quantity)                 AS units,
       sum(oi.quantity * oi.unit_price) AS revenue
FROM order_items AS oi
JOIN products   AS p   ON p.id = oi.product_id
JOIN categories AS cat ON cat.id = p.category_id
GROUP BY cat.name
ORDER BY revenue DESC;
Text
 category  | units | revenue 
-----------+-------+---------
 Equipment |     9 |  514.70
 Tea       |    28 |  334.00
 Coffee    |    22 |  290.25
(3 rows)

Two joins in a row — each JOIN adds one table, connected by its key. (The gift card has no category, so its lines dropped out of this inner join. Watch for that — Outer joins shows how to keep them.)

And filtering on a column from a joined table works just like any other condition. Orders from customers in India:

SQL
SELECT o.id, c.first_name, c.city, o.status
FROM orders AS o
JOIN customers AS c ON c.id = o.customer_id
WHERE c.country = 'India'
  AND o.status <> 'cancelled'
ORDER BY o.id;
Text
 id | first_name | city |  status   
----+------------+------+-----------
  1 | Asha       | Pune | delivered
  3 | Asha       | Pune | delivered
  8 | Farhan     | Pune | delivered
  9 | Asha       | Pune | delivered
 17 | Asha       | Pune | delivered
 22 | Farhan     | Pune | paid
 23 | Asha       | Pune | pending
(7 rows)

USING: a shortcut for same-named columns

When the key columns have the same name in both tables, USING (column) is shorthand for the ON condition and returns that column once. Bean & Leaf’s keys are named id and customer_id, so USING doesn’t fit here — but you’ll see it in databases whose keys are named the same on both sides, like JOIN order_items USING (order_id). There is also NATURAL JOIN, which joins on every column with a matching name; avoid it, because adding a column to either table can silently change what it joins on.

Other ways to write a join

Older code often lists tables with commas and puts the join condition in WHERE:

SQL
SELECT o.id, c.first_name
FROM orders o, customers c
WHERE c.id = o.customer_id
  AND o.id <= 3
ORDER BY o.id;
Text
 id | first_name 
----+------------
  1 | Asha
  2 | Ben
  3 | Asha
(3 rows)

It gives the same result, but forget the condition and you silently get every order paired with every customer. The explicit JOIN … ON style keeps join logic separate from filters and is what you should write.

The ON condition can also be anything that’s true or false — not only equality. That’s occasionally useful, for example matching orders to a table of price bands with ON o.total BETWEEN band.low AND band.high.

Tip: build joins one table at a time. Start with FROM and the first JOIN, run it, check the row count makes sense, then add the next table. When the count jumps unexpectedly, the join you just added is matching more rows than you thought.

Exercise

Who placed November's orders?

The orders table only stores a customer_id. Write a query that joins orders to customers and returns, for every order placed in November 2026, the order id, the order date as order_day (a date, no time), the customer's first_name and city, and the order status — sorted by order id. Use the table aliases o and c. You should get orders 22, 23 and 24.

SQL
SELECT o.id, o.ordered_at, o.customer_id, o.status
FROM orders AS o
WHERE o.ordered_at >= '2026-11-01'
  AND o.ordered_at <  '2026-12-01'
ORDER BY o.id;

Summary

  • A join combines rows from two tables; ON says which rows belong together, usually foreign key = primary key.
  • JOIN means INNER JOIN: only pairs that match the ON condition appear, so rows with no match on either side disappear.
  • One row on one side can match many on the other, so joins can multiply rows — one customer, many orders.
  • Give tables short aliases (FROM orders AS o) and qualify columns (o.id) — required when both tables have a column with that name.
  • After the join, WHERE, GROUP BY, HAVING and ORDER BY work on the combined rows exactly as on a single table.