Sub topic 9
One-to-Many Matches and Repeated Rows
Assume you know SELECT, WHERE, primary keys, foreign keys, NULL, and the difference between INNER JOIN and LEFT JOIN. This chapter focuses on why joins can return more rows than you expect.
A one-to-many relationship exists when one row in the first table can match several rows in the second table. For example, one customer can place many orders. A join returns one output row for each matching pair, not one row for each customer.
Why rows repeat
Suppose customer Ada has two orders. The database creates two matching pairs:
| Customer | Order |
|---|---|
| Ada | 101 |
| Ada | 102 |
Both output rows contain Ada's customer data. The repeated customer name does not mean the database stored Ada twice. It represents two different matches.
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
total DECIMAL(10, 2) NOT NULL
);
INSERT INTO customers (customer_id, name) VALUES
(1, 'Ada'),
(2, 'Ben'),
(3, 'Cleo');
INSERT INTO orders (order_id, customer_id, total) VALUES
(101, 1, 50.00),
(102, 1, 20.00),
(103, 2, 35.00);
SELECT c.name, o.order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id;The result has four rows:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 20.00 |
| Ben | 103 | 35.00 |
| Cleo | NULL | NULL |
The LEFT JOIN preserves Cleo, even though she has no matching order. Ada appears twice because two orders match her.
Predicting the result size
For each preserved row in a LEFT JOIN:
- Zero matches produce one row with
NULLvalues from the right table. - One match produces one result row.
- Several matches produce several result rows.
This matters when you add another one-to-many join. If Ada has two orders and each order has three items, joining customers to orders and then to items can produce six Ada rows. The matches multiply because each order is paired with each of its items.
Avoiding accidental duplication
Do not use DISTINCT as the first response to repeated rows. DISTINCT hides identical output values, but it does not explain whether the join is correct. First ask which entity one result row should represent.
If you need one row per customer, aggregate the orders:
SELECT c.customer_id,
c.name,
COUNT(o.order_id) AS order_count,
COALESCE(SUM(o.total), 0) AS total_spent
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name;COUNT(o.order_id) counts only real orders. Cleo receives zero. COUNT(*) would count Cleo's NULL-padded join row and return one, which is usually incorrect for an order count. COALESCE changes the NULL sum for Cleo into numeric zero.
A filter can also change which matches remain. A condition in ON limits the right-side matches while preserving customers with no qualifying order. A condition in WHERE can remove the NULL-padded row and therefore remove customers with no qualifying match.
Exercise
A customer has three orders. What happens to that customer in a LEFT JOIN that selects customer and order columns? What should you use if the result must contain one row per customer?
Show answer
The customer appears in three result rows, one for each order. Use GROUP BY with COUNT or SUM, or otherwise reduce the orders to one row before joining.