Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
QA interview question
Databases, SQL & BISQL languageMiddleCommonPractical

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

Solution · Detect join multiplication
Dialect · Portable SQLRuntime · SQLite sample DB

Runs against the resettable SQLite sample database used by the playground.

SQL
SQLite · sample DB · browser sandbox
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.

#sql#joins#cardinality#data-quality

Sources

PostgreSQL documentationPostgreSQL Global Development Group · Relational modelling, SQL, joins, constraints, transactions, concurrency, indexes, query plans and recovery