Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Window Deduplication

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL Window Deduplication. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

TransUnion's TruValidate identity-resolution pipeline can receive the same external record more than once. Write a PostgreSQL query that produces one retained record per business key for active customer accounts.

Requirements

  1. Partition records by account_id, source_system, and external_record_id.
  2. Use ROW_NUMBER() to retain the most recent record by updated_at; break timestamp ties with the greatest update_id.
  3. Use a windowed COUNT() to show how many records existed in each duplicate group. Include groups with only one record.
  4. Return only records belonging to active accounts, and sort the output by account and business key.

Schema

customer_accounts
ColumnTypeDescription
account_idPKINTEGERUnique customer account identifier
account_nameVARCHAR(100)Customer or organization name
account_statusVARCHAR(20)Current lifecycle status of the account
identity_updates
ColumnTypeDescription
update_idPKINTEGERUnique identity update identifier
account_idINTEGERCustomer account associated with the update
source_systemVARCHAR(40)System that submitted the record
external_record_idVARCHAR(60)Record identifier in the source system
updated_atTIMESTAMPTimestamp when the update was received
payload_hashVARCHAR(64)Hash identifying the submitted payload contents
Tablescustomer_accountsidentity_updates
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results