Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
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
Sign up free to run your codeLog inLn 1
Run your query to see results here.