Search GimmeJob

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

Write SQL that returns every row participating in a duplicate business key, not only the duplicated key values.

Answer

Use a window function such as COUNT(*) OVER (PARTITION BY business_key) to keep row-level detail while calculating how many rows share the key, then filter that count in an outer query. This is more useful than a plain GROUP BY when the interviewer wants the actual offending records and their primary keys.

Strong answer includes

  • uses a window function or joins back to grouped duplicate keys
  • keeps the original rows and primary keys visible
  • defines how NULL and normalized key values should be handled

Practical example

Solution · Return every duplicate row
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 rows inside each key partition, then filter in an outer query. Window values are produced after WHERE, so filter them one query level later.

Expected result: Every row whose business key occurs more than once.

#sql#duplicates#window-functions#data-quality

Sources

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