Most join bugs are not syntax errors. The query runs, returns a plausible table, and the totals are quietly wrong because rows were dropped or duplicated. Choosing the join type from the question you are answering, and checking row counts as you go, prevents almost all of them.
Start with one question: what is one row?
Before writing any join, decide what a single row of the result represents: one employee, one order, one order line, one department. Call this the grain. Then pick the table that has exactly that grain as your starting table, and treat every other table as a lookup that adds columns to it.
If the starting table is orders and you want one row per order, every table you join must match at most one row per order, or you must aggregate it first.
The decision guide
| Your question | Use |
|---|---|
| Only rows that have a match on both sides | INNER JOIN |
| All rows from my main table, with details where they exist | LEFT JOIN |
| Rows from my main table that have no match | NOT EXISTS (or LEFT JOIN + IS NULL) |
| Rows that have at least one match, listed once | EXISTS |
| Everything from both sides, matched where possible | FULL OUTER JOIN |
| Every combination of two lists | CROSS JOIN |
INNER JOIN: only matched rows
SELECT e.first_name, e.last_name, d.name AS department
FROM employees e
JOIN departments d ON d.id = e.department_id;
Employees without a department disappear. That is correct if the report is "staff by department", and a bug if the report is "all staff".
LEFT JOIN: keep every row of the main table
SELECT c.first_name, c.last_name, COUNT(o.id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.first_name, c.last_name;
Customers with no orders stay in the result with 0. Note COUNT(o.id), not COUNT(*): for an unmatched customer the joined row exists but o.id is NULL, so COUNT(*) would report 1.
EXISTS and NOT EXISTS: yes/no questions
When you only need to know whether a related row exists, do not join at all:
-- Products that have never been ordered
SELECT p.name
FROM products p
WHERE NOT EXISTS (
SELECT 1 FROM order_items oi WHERE oi.product_id = p.id
);
EXISTS cannot multiply rows, stops at the first match, and handles NULLs correctly. NOT IN (subquery) looks similar but returns nothing at all if the subquery contains a single NULL, which is why NOT EXISTS is the safer default.
FULL OUTER JOIN: reconciling two lists
SELECT d.name AS department, e.first_name
FROM employees e
FULL OUTER JOIN departments d ON d.id = e.department_id
WHERE e.id IS NULL OR d.id IS NULL;
This lists both problems at once: departments with nobody in them, and employees with no valid department. MySQL has no FULL OUTER JOIN; emulate it with a LEFT JOIN UNION a RIGHT JOIN.
The fan-out trap
The most expensive join bug happens when you join two one-to-many tables to the same parent. Suppose an order has 3 line items and 2 shipments. Joining orders to both produces 3 × 2, or 6, rows for that order, and every SUM is now inflated.
The fix is to aggregate each child table to the parent's grain first, then join the summaries:
WITH line_totals AS (
SELECT order_id, SUM(quantity * unit_price) AS revenue
FROM order_items
GROUP BY order_id
)
SELECT o.id, o.order_date, lt.revenue
FROM orders o
JOIN line_totals lt ON lt.order_id = o.id;
Each CTE now has exactly one row per order, so joining them cannot multiply anything.
Three checks that catch join bugs
- Count before and after. Run
SELECT COUNT(*)on the starting table, then on the joined result. If an INNER JOIN shrank it, rows were dropped. If any join grew it, rows were duplicated. - Count distinct keys.
COUNT(DISTINCT o.id)should equalCOUNT(*)when your grain is one row per order. If it does not, a join is fanning out. - Look at the unmatched rows. A quick
LEFT JOIN … WHERE right.id IS NULLshows exactly which rows an INNER JOIN would drop, so you can decide whether losing them is acceptable.
What not to do
- Do not add
DISTINCTto make duplicates go away. It hides the symptom, and anySUMorAVGcomputed alongside is still wrong. - Do not join on names when an id exists. Two departments can share a name; ids are unique.
- Do not mix
LEFTandRIGHTjoins in one query. RewriteRIGHT JOINas aLEFT JOINby swapping the table order, so every query reads in the same direction.