Drill 2 of 12 in Interview Classics
0

Interview Classics · GROUP BY · HAVING to find duplicates

2. Duplicate card charges

Concept: GROUP BY · HAVING to find duplicatesshow

To find duplicates, GROUP BY every column that defines 'the same thing' and keep groups with HAVING COUNT(*) > 1. Leaving a column out merges rows that aren't really duplicates.

Example (different data):

SELECT email, COUNT(*) AS copies
FROM signups
GROUP BY email
HAVING COUNT(*) > 1;

Your task

Fintech support keeps hearing 'I was charged twice'. A duplicate is the same account, merchant and amount on the same day.

Return account_id, merchant, amount, txn_date and copies for every charge that appears more than once. Sort by txn_date.

Starting Postgres in your browser…

Checking your access…