Short answer
A LEFT JOIN keeps every row from the table that is on the “left” side of the JOIN and then adds the matching rows from the “right” side.
If the join-condition matches more than one row on the right, the row from the left will be duplicated once for every match. That is why you can (and often do) end up with more rows than the left table originally contained.
──────────────────────────────────────────────────────────────────────────────
- What the engine actually does
──────────────────────────────────────────────────────────────────────────────
For every row L in the left table
find all rows R in the right table that satisfy the ON-condition
if at least one R is found → output (L ⨝ R) for every matching R ← duplicates!
if no R is found → output (L with NULLs for R’s columns)
Row count in the result =
Σ over every L ( max(1, number_of_matching_R_for_this_L) )
So whenever number_of_matching_R_for_this_L > 1 for any row, the total row
count must become larger than the number of rows in the left table.
──────────────────────────────────────────────────────────────────────────────
2. A tiny example
──────────────────────────────────────────────────────────────────────────────
Table parent
id | name
----+------
1 | Alice
2 | Bob
Table child
id | parent_id | toy
----+-----------+------
11 | 1 | Lego
12 | 1 | Doll
13 | 2 | Ball
14 | 2 | Car
15 | 2 | Kite
SELECT *
FROM parent p
LEFT JOIN child c ON c.parent_id = p.id;
Result → 2 parents, but 5 rows:
id | name | id | parent_id | toy
----+-------+----+-----------+------
1 | Alice | 11 | 1 | Lego
1 | Alice | 12 | 1 | Doll
2 | Bob | 13 | 2 | Ball
2 | Bob | 14 | 2 | Car
2 | Bob | 15 | 2 | Kite
──────────────────────────────────────────────────────────────────────────────
3. The most common reasons you see “too many” rows
──────────────────────────────────────────────────────────────────────────────
• The join column on the right side is NOT unique (no PK/unique index).
Even if it should logically be unique, bad data will still duplicate rows.
• The join condition is incomplete:
ON a.col1 = b.col1 ↩︎ but col2, col3 also need to be compared!
• You accidentally wrote a cross-join (forgot the ON condition).
• You joined through a view that itself does other joins ⇒ fan-out.
──────────────────────────────────────────────────────────────────────────────
4. How to find & fix the culprit
──────────────────────────────────────────────────────────────────────────────
a) Count matches per left-row
SELECT left_key, count() right_matches
FROM left_table l
LEFT JOIN right_table r ON ( … )
GROUP BY left_key
HAVING count() > 1; -- these rows are getting duplicated
b) Check / enforce uniqueness on the right side
ALTER TABLE right_table ADD CONSTRAINT … UNIQUE (join_column[, …]);
c) Tighten the join predicate
ON a.id = b.id
AND a.version = b.version -- for example
d) If you only care about “exists” rather than “how many”
- Use EXISTS (semi-join) instead of LEFT JOIN
- Or aggregate first: join to a sub-query that returns one row per key.
e) As a last resort, collapse duplicates
SELECT DISTINCT …
or
SELECT … GROUP BY …
──────────────────────────────────────────────────────────────────────────────
5. Quick checklist
──────────────────────────────────────────────────────────────────────────────
☑ Do the columns in ON (…) uniquely identify a row of the right table?
☑ Does right_table have a UNIQUE or PRIMARY KEY covering those columns?
☑ Did I forget an extra join condition?
☑ Do I really want all matches, or just “does at least one match exist”?
If after these steps you still see “extra” rows, post the actual tables,
sample data, and the exact query and we can dig deeper!