JOIN example
SELECT p.id AS order_record,
c.name AS customer,
p.total
FROM orders AS p
JOIN customers AS c
ON c.id = p.customer_id
ORDER BY p.id
LIMIT 6;| order_record | customer | total |
|---|---|---|
| 101 | Amy Wilson | 128.4 |
| 102 | Liam Martin | 49.5 |
| 103 | Amy Wilson | 24.9 |
| 104 | Megan Reed | 179 |
| 105 | David Clark | 69.9 |
| 106 | Sara Green | 1088 |
Each order contains customer_id. The condition compares it with the primary key of customers to fetch the matching name.
How JOIN is used
ON decides which rows are paired. Aliases make it clear which table each column comes from and avoid ambiguities such as two columns called id.
What changes from case to case
Why a row can appear more than once
A customer can have several orders. That is why Amy appears in more than one row: it is not an accidental duplicate but the correct result of a one-to-many relationship.
Before “fixing duplicates”, work out the cardinality. If a row on the left has three valid matches on the right, the result needs three rows. Using DISTINCT without understanding the cause can hide a join error.
Choose the type based on the question
INNER JOIN: matching pairs only.LEFT JOIN: keeps every row from the left.CROSS JOIN: produces every combination.- Self join: relates a table to itself through different aliases.
Common mix-ups with JOIN
Joining columns that share a name but not a meaning
ON customers.id = orders.idBoth columns are ids, but they refer to different entities. The correct relationship is customers.id = orders.customer_id. Check the keys and the data model before writing ON.
Before joining, decide what each result row represents
A JOIN combines rows from two sources whenever a condition is met. Each matching pair produces one row, so the number of rows in the result depends on the relationship between the tables, not on how many rows each has on its own. If a customer has three orders, they appear three times: that is the expected behaviour, and you should decide whether that is the level of detail you need.
The condition ON must relate columns that mean the same thing, usually a foreign key and the key it references. Comparing two ids from different entities can produce accidental matches that look correct. Also choose the join type based on which rows should survive: INNER JOIN drops the rows with no match; LEFT JOIN keeps those on the left, with missing values on the right.
To validate a join, count the rows before and after with a test query and look for repeated keys on the side that should be unique. A figure that doubles when you add a table usually points to multiplied rows, not wrong data. If you need a summary per customer, group after joining and count a key from the side you want to measure.
Self-contained case to check in SQLite
Amy has two orders and Liam has one. We count the rows the join produces for each customer.
WITH customers(id, name) AS (
VALUES (1, 'Amy'), (2, 'Liam')
), orders(id, customer_id, total) AS (
VALUES (10, 1, 30), (11, 1, 20), (12, 2, 15)
)
SELECT c.name, COUNT(*) AS rows, SUM(p.total) AS amount
FROM customers c
JOIN orders p ON p.customer_id = c.id
GROUP BY c.name
ORDER BY c.name;| name | rows | amount |
|---|---|---|
| Amy | 2 | 50 |
| Liam | 1 | 15 |
Each order forms a pair with its customer. Amy's name repeats in the join, but grouping brings it back to one row per customer.
Check your interpretation: How many rows would the join produce if Amy also had three payments in another table joined by customer?
Show answer
Amy would have 6 rows: each of her two orders would be paired with each of her three payments (2 × 3). SUM(p.total) would give 150 instead of 50, because each order would be repeated three times.
Technical questions: PostgreSQL: Table Expressions and Joins (opens in a new tab).