Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Deduping Vendor Imports

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

Your question is SQL Deduping Vendor Imports. 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 find duplicate records in a vendor import table and keep only the most recent row.

Treat rows with the same vendor_code, invoice_number, and line_number as duplicates. If duplicate rows have the same import timestamp, retain the row with the highest import_id.

Output

  1. Return one retained row per logical vendor invoice line.
  2. Include import_id, vendor_code, invoice_number, line_number, amount, imported_at, and source_file.
  3. Order by vendor_code, invoice_number, line_number, and import_id ascending.

Schema

vendor_imports
ColumnTypeDescription
import_idPKINTUnique identifier for the imported row
vendor_codeVARCHAR(20)Vendor identifier from the import
invoice_numberVARCHAR(30)Vendor invoice identifier
line_numberINTLine number within the invoice
amountDECIMAL(12,2)Amount reported for the invoice line
imported_atTIMESTAMPTimestamp when the row entered the import table
source_fileVARCHAR(100)Name of the source file that supplied the row
Tablesvendor_imports
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results