Search GimmeJob

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

How would you use SQL to find duplicate business keys in a table?

Answer

Group by the business-key columns and filter groups with HAVING COUNT(*) greater than one, for example SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1. First define whether case, whitespace, NULL and historical versions should count as the same key. Then inspect the underlying rows, confirm the intended uniqueness rule and avoid deleting anything until ownership, retention and recovery are clear.

Strong answer includes

  • uses GROUP BY with HAVING COUNT
  • clarifies the real business key
  • separates detection from destructive cleanup

Practical example

Find duplicate normalized business keys
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.

GROUP BY collapses rows to the business-key grain and HAVING filters after the count is calculated. Normalization makes the query match a rule where case and surrounding whitespace are insignificant.

Expected result: One row per duplicated normalized email, with the number of copies.

#sql#database#duplicates#data-quality

Sources

PostgreSQL documentationPostgreSQL Global Development Group · Relational modelling, SQL, joins, constraints, transactions, concurrency, indexes, query plans and recoveryAdd data tests to your DAGdbt Labs · Reusable assertions for analytics transformations and data contracts