Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Deduping Latest Records
00:00
5 left

SQL Deduping Latest Records

MediumSQL · PostgreSQL

Problem

In SQL, how would you write a query to find duplicate records and keep only the latest row per group?

Use property_id as the duplicate grouping key. A group qualifies only when it contains more than one record. Return the latest record by updated_at, using the greatest record_id as the tie-breaker.

Output

  1. One row for each duplicate group, containing the retained record and its duplicate_count
  2. Columns: record_id, property_id, listing_status, price, updated_at, source_system, duplicate_count
  3. Exclude unique groups and order by property_id ascending

Schema

property_listing_records
ColumnTypeDescription
record_idPKINTUnique identifier for the ingested listing record
property_idINTIdentifier used to group records for the same property
listing_statusVARCHAR(30)Current status reported for the listing
priceDECIMAL(12,2)Listing price at ingestion time
updated_atTIMESTAMPTimestamp when the source listing was last updated
source_systemVARCHAR(40)System that supplied the record
Tablesproperty_listing_records
Interviewer

Your question is SQL Deduping Latest 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.