Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Query to Remove Duplicate Rows

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

Your question is SQL Query to 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 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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results