Sub topic 3
One-to-many matches and repeated rows
This chapter assumes you know SELECT, WHERE, INNER JOIN, GROUP BY, how NULL behaves in comparisons, and how primary and foreign keys link tables. The examples use the customers and orders tables from the chapter on NULL-padded rows.
How one-to-many joins repeat rows
A customer can have many orders, so each customer row matches several order rows. The join returns one result row per match, which repeats the customer's columns.
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id;
-- Ada 101
-- Ada 102
-- Ben 103
-- Cleo NULLAda appears twice. Count rows per customer, not rows in the result, when you need a per-customer figure.
Counting and summing with outer joins
COUNT(*) counts rows, including NULL-padded rows. COUNT(column) skips NULLs. Use a right-table column so unmatched customers count as zero. SUM returns NULL when every input is NULL, so wrap it in COALESCE.
SELECT c.name,
COUNT(o.order_id) AS order_count,
COALESCE(SUM(o.total), 0) AS total_spent
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name;
-- Ada 2 70.00
-- Ben 1 35.00
-- Cleo 0 0.00Chaining several joins
Each outer join must keep the rows from the earlier steps. Add a payments table to see the effect.
CREATE TABLE payments (
payment_id INT PRIMARY KEY,
order_id INT REFERENCES orders (order_id),
amount DECIMAL(10, 2) NOT NULL
);
INSERT INTO payments VALUES (1, 101, 50.00), (2, 103, 35.00);
SELECT c.name, o.order_id, p.amount
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
LEFT JOIN payments p ON p.order_id = o.order_id;
-- Ada 101 50.00
-- Ada 102 NULL
-- Ben 103 35.00
-- Cleo NULL NULLAn inner join on payments would remove Ada's order 102 and Cleo. Keep every join in the chain outer unless you want that loss.
FULL OUTER JOIN without native support
FULL OUTER JOIN is missing in MySQL. Combine a LEFT JOIN with the right-only rows from a RIGHT JOIN. UNION ALL keeps duplicates, so it does not remove the matched rows twice.
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL;NOT EXISTS and NOT IN
NOT EXISTS finds customers with no orders and states that intent directly.
SELECT c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
);
-- CleoNOT IN looks similar but fails with NULLs. Order 104 has a NULL customer_id, so the following query returns no rows:
SELECT c.name
FROM customers c
WHERE c.customer_id NOT IN (SELECT o.customer_id FROM orders o);The comparison with NULL is unknown, so no row qualifies. Prefer NOT EXISTS for anti-joins.
Exercise
How many rows does the LEFT JOIN of customers to orders return, and why?
Show answer
Four rows. Ada matches two orders, so she appears twice. Ben matches one order. Cleo has no match and appears once with NULL in the order columns.