Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Deduplicate Policy Records
00:00
5 left

Deduplicate Policy Records

MediumSQL · PostgreSQL

Problem

Write a SQL query to identify duplicate policy records within a database and retain only the most recently updated entry.

Treat records with the same policy_number as duplicates. If update timestamps are tied, retain the record with the greatest policy_id. Do not include older duplicate records.

Output

  1. One row per retained policy number, with columns policy_id, policy_number, policyholder_name, policy_status, premium_amount, and updated_at.
  2. Include the most recently updated record for every policy number, including policy numbers without duplicates.
  3. Order by policy_number ascending.

Schema

policies
ColumnTypeDescription
policy_idPKINTEGERUnique identifier for a policy record version
policy_numberVARCHAR(30)Business identifier used to detect duplicate policy records
policyholder_nameVARCHAR(100)Name of the policyholder
policy_statusVARCHAR(20)Current status recorded for the policy version
premium_amountNUMERIC(12,2)Policy premium amount
updated_atTIMESTAMPTimestamp when the policy record was last updated
Tablespolicies
Interviewer

Your question is Deduplicate Policy Records. Start with the requirements and the one table in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.