Sub topic 10
Spotting an Accidental Cartesian Product
Assume you know INNER JOIN, LEFT JOIN, ON, primary keys, and foreign keys. You should also be comfortable checking row counts and reading NULL values.
A Cartesian product pairs every row from one input with every row from another. If table A has 4 rows and table B has 3 rows, the result has:
4 × 3 = 12 rows
A Cartesian product is intentional when you need every possible combination. It is accidental when a query was supposed to match related rows but does not contain a correct matching condition.
How the mistake happens
A normal join uses an ON condition to restrict pairs:
SELECT e.name, d.department_name
FROM employees AS e
INNER JOIN departments AS d
ON e.department_id = d.id;Each employee matches the department whose id equals the employee's department_id.
If the condition is omitted, every employee is paired with every department:
SELECT e.name, d.department_name
FROM employees AS e
CROSS JOIN departments AS d;This query is valid SQL. It is not automatically an error. The question is whether all combinations are wanted.
A more dangerous mistake uses multiple tables in the FROM clause without a relationship condition:
SELECT e.name, d.department_name
FROM employees AS e, departments AS d;The comma syntax is an older way to request a Cartesian product. A developer might intend an inner join but forget to add the condition in the WHERE clause.
SELECT e.name, d.department_name
FROM employees AS e, departments AS d
WHERE e.department_id = d.id;This produces the intended matching result, but explicit JOIN ... ON syntax makes the relationship easier to see and review.
Recognizing the symptoms
Look for these warning signs:
- The row count is much larger than expected.
- The result repeats the same value from one table beside many unrelated values from another.
- Removing one table causes the row count to drop by a predictable multiplication factor.
- A join has no
ONclause, or its condition compares unrelated columns. - A self join has a condition that is always true, such as
a.id = a.id.
The expected and actual row counts help diagnose the problem.
| Input sizes | Expected matching rows | Possible Cartesian rows |
|---|---|---|
| 100 employees, 5 departments | About 100 | 500 |
The expected matching count depends on the data. The Cartesian count does not: it is always the product of the input sizes.
Fixing the query
Start by stating the relationship in plain language: “An employee belongs to the department whose identifier equals the employee's department identifier.” Then write that relationship in the ON clause.
For a self join, qualify both sides with aliases:
SELECT e.name AS employee, m.name AS manager
FROM employees AS e
INNER JOIN employees AS m
ON e.manager_id = m.id;Here, e and m are two roles for the same table. The ON condition prevents every employee from pairing with every other employee.
Do not add DISTINCT merely to hide unexpected duplicates. DISTINCT removes duplicate output rows, but it does not repair incorrect pairings. Find the missing or incorrect relationship condition instead.
A deliberate Cartesian product should be documented by its purpose, such as generating every store-product combination. Otherwise, prefer explicit join syntax and check the result count.
Exercise
A query joins 8 customers to 4 discount levels and returns 32 rows. The requirement is to show only the discount level assigned to each customer. What should you inspect first?
Show answer
Inspect the join condition. The query probably has a missing or incorrect condition linking each customer's assigned discount level to the discount-level table.