How would you use SQL to detect that a JOIN unexpectedly multiplied parent rows?
Answer
Compare the parent population before and after the join at the intended grain, and group the joined result by the parent primary key to find keys that appear more than once. Typical causes are a legitimate one-to-many relationship, an incomplete join predicate, duplicated dimension rows or joining at the wrong grain; DISTINCT may hide the symptom without fixing the logic.
Strong answer includes
- checks row counts and parent-key multiplicity before and after the join
- distinguishes one-to-many behavior from an incomplete join condition
- does not use DISTINCT as the default repair
Practical example
Dialect · Portable SQLRuntime · SQLite sample DB
Runs against the resettable SQLite sample database used by the playground.
SQL
Result
Run the query to see the result.
Count joined rows per expected parent key and inspect groups with more than one match. Multiple rows may be correct for 1:N; the defect is assuming 1:1 or omitting part of a composite join key.
Expected result: Parent keys whose joined cardinality is greater than one.