Skip to content

13 min read

SQL joins explained with examples

In one sentence: A JOIN puts together the rows of two tables that match a condition, like each order with the product whose id it carries. It isn't the overlap of a Venn diagram: it pairs up rows, and if a row matches two, it comes out twice.

What you will learn

  • Explain why the shop's data is in several tables, linked by primary and foreign keys.
  • Put orders and products together with INNER JOIN, and use LEFT JOIN to find the products nobody has ordered.
  • Join three tables through an intermediate table.
  • Work out how many rows a JOIN returns, and explain why there can be more than in any of its tables.

Before you start, read: 01-sql.md

The delivery company from Phase 4 wants to print a label for each order: “Order 1: 2 × Mug.” You ask the database for the orders, and this is what’s there:

SELECT id, product_id, quantity, status FROM orders ORDER BY id LIMIT 3;
id | product_id | quantity | status
----+------------+----------+---------
1 | 2 | 2 | shipped
2 | 3 | 3 | shipped
3 | 1 | 4 | shipped
(3 rows)

No sign of “Mug.” Order 1 says product_id 2, and the name is in another table, products. That’s on purpose: the Mug appears in 9 of the 25 orders. If each order copied its name and its price, renaming it to “Large mug” would mean changing 9 rows, and if you forgot one, the shop would have two names for the same mug.

Each piece of data lives in a single place, and the order only points to it. For the label, you have to put them back together: that’s a Join (combining tables)The part of an SQL query that puts together the rows of two tables that match on a condition, such as each order with its product. If a row matches several in the other table, it comes out once for each one.Go to definition.

The analogy

Luis writes the orders down in a notebook. Each line says “product no. 2, 2 units.” On the first page he has the price list: “no. 1: T-shirt, €20; no. 2: Mug, €8.50…” To pack a parcel, he looks at the number on the line and finds it on the first page.

If the Mug goes up in price, he changes a single line on the first page, not every order. And if he wants to write next to each order its product’s category, and the Mug is in “Home” and in “Gifts,” that order takes up two lines: one per category.

Where the analogy breaks down

  • Luis searches by eye, order by order. The database chooses how to find the pairs and, with an Index (database)A separate, sorted list with the values of one or more columns of a table and where each row is. With it, the database finds a few rows without reading the whole table. In exchange, it takes up disk space and every write has to update it.Go to definition, finds them without looking at everything (you’ll see it in lesson 7, “Indexes”).
  • Luis would notice he had copied an order twice. The database wouldn’t: it returns every pair, and whether that’s what you wanted is up to you.
  • Nothing stops Luis from writing down “product no. 99,” even if it doesn’t exist. The database does stop it, as you’ll see in a moment.

The QueryA command written in SQL for a database. Mostly the ones that ask for data (SELECT), whose answer is another table, although the ones that add, change or delete rows are called that too.Go to definition in this lesson use the shop’s data (shop.sql) freshly loaded, as in lesson 1. The outputs are from PostgreSQL 18.6.

Each row has a unique number: the primary key

Section titled “Each row has a unique number: the primary key”

This is how the products table starts in shop.sql:

CREATE TABLE products (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,

PRIMARY KEY makes id the Primary keyThe column, or group of columns, that identifies each row of a table. Its value can’t repeat or be missing, like an order number.Go to definition: the column that identifies each row. Its value is never repeated and can’t be missing. GENERATED ALWAYS AS IDENTITY asks PostgreSQL to fill in the number itself, 1, 2, 3…, as each product is inserted.

A primary key can also be two columns. In product_categories, which says which categories each product is in, it’s the pair (product_id, category_id): the T-shirt can be in Clothing once, not twice. If you try it (with no column list, the values go in the table’s order):

INSERT INTO product_categories VALUES (1, 1);
ERROR: duplicate key value violates unique constraint "product_categories_pkey"
DETAIL: Key (product_id, category_id)=(1, 1) already exists.

The pair (1, 1) already exists. A ConstraintA rule the database checks on every change, such as the price being greater than zero or no two categories having the same name. Data that breaks it doesn’t get in, wherever it comes from.Go to definition is a rule that PostgreSQL checks on every change, and the primary key is one.

An order points to its product: the foreign key

Section titled “An order points to its product: the foreign key”

In orders, the column that points to the product is declared like this:

product_id integer NOT NULL REFERENCES products (id),

REFERENCES products (id) turns it into a Foreign keyA column that stores the primary key of a row in another table, such as the product number of each order. The database won’t let you save a value that doesn’t exist in the other table and, unless told otherwise, won’t let you delete a row that others point to either.Go to definition: its values have to exist in the id column of products. See what happens if someone creates an order for product 99:

INSERT INTO orders (product_id, quantity, status, created_at)
VALUES (99, 1, 'pending', now());
ERROR: insert or update on table "orders" violates foreign key constraint "orders_product_id_fkey"
DETAIL: Key (product_id)=(99) is not present in table "products".

The database refuses: product 99 isn’t in products. now() is the date and time at this moment.

It works the other way around too. DELETE FROM products WHERE id = 2; fails with Key (id)=(2) is still referenced from table "orders": the Mug has orders pointing to it. What to do in that case is chosen when the table is created: forbid it, as here, or delete those rows too, as shop.sql does with a product’s categories. You’ll see it in lesson 4, “Modeling”.

The foreign key guarantees that every order points to a product that exists. Without it, a bug in the program would leave orphan orders, and a JOIN wouldn’t find their product.

INNER JOIN puts each order together with its product

Section titled “INNER JOIN puts each order together with its product”

This is the query for the labels:

SELECT o.id, p.name, o.quantity
FROM orders AS o
JOIN products AS p ON p.id = o.product_id
ORDER BY o.id
LIMIT 5;
id | name | quantity
----+---------+----------
1 | Mug | 2
2 | Hoodie | 3
3 | T-shirt | 4
4 | Mug | 1
5 | Hoodie | 2
(5 rows)

Word by word:

  • FROM orders AS o: the first table, the orders. AS o gives it a short name, an alias.
  • JOIN products AS p: the second table, with the alias p. Plain JOIN is INNER JOIN; both forms work.
  • ON p.id = o.product_id: the condition. A row from orders and one from products go together when the product’s id is the order’s product_id.
  • SELECT o.id, p.name, o.quantity: the columns, each with the table it comes from in front. o.id is the order’s id, not the product’s.
  • ORDER BY and LIMIT, the ones from lesson 1.

For each order, PostgreSQL looks for the products that meet the condition, and each pair that matches is a row of the result. Order 1 carries 2, and product 2 is the Mug: row “1, Mug, 2”.

INNER means that only the pairs come out. A product with no orders, like the Cap, doesn’t appear.

Both tables have an id column. If you don’t say which one you mean, PostgreSQL doesn’t guess:

SELECT id, name, quantity
FROM orders
JOIN products ON products.id = orders.product_id;
ERROR: column reference "id" is ambiguous
LINE 1: SELECT id, name, quantity
^

PostgreSQL can’t tell which id you mean, and the ^ points to where. name and quantity don’t cause an error, because each only exists in one of the two. You fix it with the table’s name in front: orders.id.

With aliases, instead of orders.id you write o.id. The AS is optional (FROM orders o works too), but it reads better. An alias only lasts as long as the query: it doesn’t change the table’s name.

LEFT JOIN also keeps the rows without a match

Section titled “LEFT JOIN also keeps the rows without a match”

Which product has nobody ordered? With INNER JOIN you can’t see it: the product with no orders has no pair, and it disappears. That’s what LEFT JOIN is for:

SELECT p.name, o.id AS order_id
FROM products AS p
LEFT JOIN orders AS o ON o.product_id = p.id
ORDER BY o.id DESC
LIMIT 3;
name | order_id
---------+----------
Cap |
Mug | 25
T-shirt | 24
(3 rows)

LEFT JOIN returns the same pairs as INNER JOIN and, on top of that, each row of the left table that matches nothing, once, with the right table’s columns empty. The left one is the one before the JOIN: here, products. The Cap has no orders, and it comes out with order_id empty: it’s NULL, the missing value from lesson 1 (the playground writes it NULL).

o.id AS order_id changes the column’s name in the result. DESC sorts from highest to lowest, and PostgreSQL puts the NULLs first in that order: that’s why the Cap tops the list.

To keep only the ones without a match, filter by that empty value:

SELECT p.name
FROM products AS p
LEFT JOIN orders AS o ON o.product_id = p.id
WHERE o.id IS NULL;

WHERE o.id IS NULL keeps the rows where no order turned up. One comes out: Cap.

There’s also RIGHT JOIN, the same with the tables the other way around, and FULL JOIN, which keeps the unmatched rows on both sides. You almost always write LEFT JOIN, with the table you want whole on the left.

Three tables: the products and their categories

Section titled “Three tables: the products and their categories”

A product can be in several categories, and a category has several products. That’s why neither products nor categories stores the relationship: it’s in a third table, product_categories, with one row per pair. To read the names, you need two JOINs in a row:

SELECT p.name, c.name AS category
FROM products AS p
JOIN product_categories AS pc ON pc.product_id = p.id
JOIN categories AS c ON c.id = pc.category_id
ORDER BY p.id, c.name;
name | category
---------+----------
T-shirt | Clothing
Mug | Gifts
Mug | Home
Hoodie | Clothing
Cap | Clothing
Cap | Gifts
(6 rows)
  • The first JOIN pairs each product with its rows in product_categories.
  • The second pairs each of those rows with its category, by category_id.
  • ORDER BY p.id, c.name sorts by product and, within each one, by category.

Four products and six rows: the Mug and the Cap come out twice, once per category. Why this intermediate table is the right way to store the relationship, you’ll see in lesson 4, “Modeling”.

A JOIN pairs up rows instead of intersecting sets

Section titled “A JOIN pairs up rows instead of intersecting sets”

If you search for “SQL JOIN” in images, you’ll see overlapping circles: two tables, and the INNER JOIN is the shared area. That drawing suggests the result never has more rows than the smallest table. It’s false. Count the rows of each query in this lesson (COUNT(*) counts the rows of the result, and you’ll see it in depth in lesson 3, “GROUP BY”):

Query Rows
orders alone 25
products alone 4
orders JOIN products 25
products LEFT JOIN orders 26
orders JOIN product_categories (each order with its product’s categories) 34

orders JOIN products gives 25, more than the 4 products: each order finds its product, and the Mug comes out in 9 rows. And with the categories, 34, more than either of the two tables:

SELECT o.id, p.name, c.name AS category
FROM orders AS o
JOIN products AS p ON p.id = o.product_id
JOIN product_categories AS pc ON pc.product_id = p.id
JOIN categories AS c ON c.id = pc.category_id
ORDER BY o.id, c.name
LIMIT 4;
id | name | category
----+---------+----------
1 | Mug | Gifts
1 | Mug | Home
2 | Hoodie | Clothing
3 | T-shirt | Clothing
(4 rows)

Order 1 comes out twice. The math: 9 Mug orders times 2 categories, 18 rows; plus 8 Hoodie orders and 8 T-shirt orders, with one each; 34.

The rule: each row of one table comes out as many times as there are rows in the other that match it. If it matches none, zero times in an INNER JOIN (in a LEFT JOIN, once, with empty values); once if it matches one, and several times if it matches several. The Venn diagram only gets right which rows survive, not how many times they come out.

This matters when you count or add up: if someone adds up the units sold after joining orders with categories, each mug counts twice.

The goal: prepare the labels for the paid orders, find out which product nobody has bought and predict how many rows a JOIN returns before running it.

Each query appears twice: in the playground, which doesn’t need anything installed, and in psql, the PostgreSQL on your computer. Open psql -d shop. If you deleted or changed data in lesson 1, load shop.sql again as in the introduction; in the playground, press “Reset data”.

playground · PostgreSQL in your browser

A real PostgreSQL, with the shop’s data. What you change stays on this page.

Ctrl + Enter runs it too.

1. The labels, in psql

Ventana de terminal
SELECT o.id, o.quantity, p.name
FROM orders AS o
JOIN products AS p ON p.id = o.product_id
WHERE o.status = 'paid'
ORDER BY o.id;

What you will see:

id | quantity | name
----+----------+---------
13 | 2 | Mug
14 | 3 | Hoodie
15 | 4 | T-shirt
16 | 1 | Mug
17 | 2 | Hoodie
18 | 3 | T-shirt
19 | 4 | Mug
20 | 1 | Hoodie
(8 rows)

Type it after the shop=> prompt. psql doesn’t run it until the ;: if you press Enter before, the prompt changes to shop->, which means “I’m still waiting.” Eight paid orders, from 13 to 20, each with its name. The WHERE applies to the rows already joined, so it can use columns from both tables. If you get relation "orders" does not exist, you haven’t loaded shop.sql or you’re in another database: check that the prompt starts with shop.

Now change it: ask for the 'pending' ones, or add p.price_cents to the columns to put the price on the label.

playground · PostgreSQL in your browser

A real PostgreSQL, with the shop’s data. What you change stays on this page.

Ctrl + Enter runs it too.

2. The products with no orders, in psql

Ventana de terminal
SELECT p.name
FROM products AS p
LEFT JOIN orders AS o ON o.product_id = p.id
WHERE o.id IS NULL;

What you will see:

name
------
Cap
(1 row)

One row: the Cap. Change LEFT JOIN to JOIN and run it again: you get (0 rows), because INNER JOIN has already removed the Cap before the WHERE looks for it.

Challenge: before running it, work out how many rows the join of products with product_categories returns (without categories). Then check it with SELECT COUNT(*) FROM products AS p JOIN product_categories AS pc ON pc.product_id = p.id;Show answer

Six. Each product comes out as many times as it has categories: T-shirt 1, Mug 2, Hoodie 1 and Cap 2. They’re the six rows of product_categories, because each one matches a single product:

count
-------
6
(1 row)
  • The confirmation email for a purchase carries each product’s name and photo, even though the order only stores its number: someone did a JOIN to write it.
  • VLOOKUP in a spreadsheet is a JOIN done by hand: for each row, it looks up its value in another table and brings back a column. The difference: VLOOKUP stops at the first match, and a JOIN returns them all.
  • The shop’s API returns productId in each order, and in the GraphQL lesson the app had to make one more request per product. With a JOIN on the server, the name can arrive in the same response.
  • Forgetting the condition. JOIN without ON is a syntax error in PostgreSQL, but the old form, FROM orders, products, without a WHERE, pairs every order with every product: 100 rows (25 times 4), almost all of them wrong.
  • Using INNER JOIN when you want them all. A sales report per product with INNER JOIN has no row for the Cap: it looks as if it doesn’t exist, when what’s happening is that it sells zero.
  • Filtering the right table of a LEFT JOIN in the WHERE. With LEFT JOIN orders … WHERE o.status = 'pending', the Cap disappears again, because its status is empty. If you want “each product with its pending orders, if it has any,” put the condition in the ON: ON o.product_id = p.id AND o.status = 'pending'.
  • Adding up after a JOIN that multiplies. If a row comes out twice, its numbers count twice.
  • Writing id when there are two. Ambiguous column: put the table or its alias in front.
  • The shop’s data is in several tables so that each piece lives in a single place. The primary key identifies each row, and a foreign key points to another table’s.
  • REFERENCES won’t let you save an order for a product that doesn’t exist nor, unless something else is said when creating the table, delete a product that has orders.
  • INNER JOIN returns the pairs of rows that meet the ON condition; LEFT JOIN, on top of that, the rows on the left with no match, with empty values.
  • With an intermediate table, two JOINs in a row join products and categories.
  • A JOIN pairs up rows: each row comes out as many times as there are rows in the other table that match it. The result can have more rows than any of its tables.
Ana wants to rename “Hoodie” to “Zip-up hoodie.” With the shop’s data, how many rows does she have to change?

A program with a bug tries to save an order with product_id 7, and products only has 1 to 4. What happens?

You ask for each category with its products: categories JOIN product_categories JOIN products. There are 3 categories and 4 products. How many rows come out?

You make a report with each product and its number of orders, and the Cap doesn’t appear. What happened?

You write products LEFT JOIN orders … WHERE o.status = 'paid' to see each product with its paid orders, if it has any. Does the Cap come out?

You join products, product_categories and categories, and ask for SELECT name. PostgreSQL answers column reference "name" is ambiguous. What do you fix?

phase-5 · lesson 2/10