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
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 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.