Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Query to Remove Duplicate Rows
00:00
5 left

SQL Query to Remove Duplicate Rows

MediumSQL · PostgreSQL

Problem

Write a query to identify and remove duplicate rows from a high-volume table.

Use payment_transactions. Treat payment_id as the unique row identifier and all other columns as the duplicate-detection fields. Retain the row with the smallest payment_id in each duplicate set.

Output

  1. One row per deleted duplicate, with column payment_id
  2. Include only rows removed by the query
  3. Sort by payment_id ascending

Schema

payment_transactions
ColumnTypeDescription
payment_idPKINTEGERUnique payment transaction identifier
merchant_idVARCHAR(30)Merchant identifier
customer_idVARCHAR(30)Customer identifier, when available
amountNUMERIC(12,2)Payment amount
currencyVARCHAR(3)ISO currency code
payment_statusVARCHAR(40)Current payment processing status
paid_atTIMESTAMPPayment completion timestamp
Tablespayment_transactions
Interviewer

Your question is SQL Query to Remove Duplicate Rows. 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.