Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Remove Duplicate Rows

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

Your question is SQL Remove Duplicate Rows. Start with the requirements and the one table 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

Write a query to remove duplicate rows from a table while keeping the latest record.

Use records and treat rows with the same customer_id and event_type as duplicates. The latest row is determined by recorded_at, with record_id as the tie-breaker when timestamps match.

Output

  1. Delete every duplicate while retaining one latest row per business key.
  2. Return deleted record_id, customer_id, and event_type values.
  3. Ordering is not required for the returned rows.

Schema

records
ColumnTypeDescription
record_idPKINTUnique record identifier
customer_idINTCustomer associated with the event
event_typeVARCHAR(30)Type of customer event
payloadTEXTEvent data stored with the record
recorded_atTIMESTAMPTZTime when the event was recorded
Tablesrecords
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results