Sub topic 17
Common Mistakes and How to Debug a Join
This chapter assumes you know SELECT, FROM, WHERE, ORDER BY, primary keys, and foreign keys. It also assumes the example database contains customers, orders, and order_items.
An inner join returns only row pairs that satisfy its ON condition. Most join bugs come from an incorrect condition, unexpected row multiplication, or a filter that removes rows later than expected.
Check the join condition first
A missing or incomplete ON condition can create a Cartesian product: every row from one table is paired with every row from the other table.
-- Wrong: every customer is paired with every order.
SELECT c.name, o.id AS order_id
FROM customers AS c
CROSS JOIN orders AS o;Use the foreign key and primary key that represent the relationship:
SELECT c.name, o.id AS order_id
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id;Qualify column names with aliases. This prevents ambiguity and makes it clear which table supplies each value.
Compare row counts while debugging
Build the query in stages. First count each table, then count after each join.
SELECT COUNT(*) AS customer_count
FROM customers;
SELECT COUNT(*) AS order_count
FROM orders;
SELECT COUNT(*) AS joined_count
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id;A sudden, unexpected increase usually means the join is matching multiple rows. A sudden decrease is expected for an inner join when some rows have no match.
Check the key for duplicates:
SELECT customer_id, COUNT(*) AS order_count
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 1;Multiple orders for one customer are valid. They mean that one customer row appears once for each matching order.
Watch for NULL and missing matches
NULL means an unknown or absent value. SQL does not treat NULL = NULL as true. Therefore, an order with customer_id IS NULL cannot match a customer through a normal equality condition.
SELECT id, customer_id
FROM orders
WHERE customer_id IS NULL;These orders disappear from an inner join. If the question is “show every customer, including those with no orders,” use a left outer join instead.
Avoid double counting after a join
Joining a parent to several child rows repeats the parent values. This query can overcount order totals:
SELECT SUM(o.total) AS inflated_revenue
FROM orders AS o
INNER JOIN order_items AS i
ON i.order_id = o.id;Aggregate each order before joining, or aggregate distinct order IDs in a separate query.
SELECT SUM(o.total) AS revenue
FROM orders AS o
WHERE EXISTS (
SELECT 1
FROM order_items AS i
WHERE i.order_id = o.id
);The EXISTS condition checks for a matching item without copying the order once per item.
Use a repeatable debugging path
For a performance problem, inspect the execution plan:
EXPLAIN
SELECT c.name, o.id
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id
WHERE o.total > 30;Look for a scan of a very large table when an index could help. An index on orders.customer_id commonly helps this relationship, but the database optimizer decides whether to use it.
Exercise
A customer has three orders, and each order has four items. After joining customers, orders, and order_items, how many rows represent that customer?
Show answer
Twelve rows. Each of the three orders matches four item rows, so the join produces 3 × 4 rows.