← Problems15. Find duplicate invoicesEasySQL
00:00 / 20:00

Find duplicate invoices

Easy54% solve itAsked at Walmart, Deloitte

An upstream loader retried without idempotency, so some invoices were inserted more than once. Two rows are duplicates when vendor_id, invoice_number and amount all match. Return one row per duplicate group with the number of copies and the earliest loaded_at.

Input schema

invoices invoice_id bigint vendor_id bigint invoice_number string amount numeric loaded_at timestamp

Constraints

  • Groups with a single row are not duplicates and must not appear
  • Order by copies descending, then vendor_id, then invoice_number
  • Up to 40,000,000 rows

Example 1one duplicated invoice

Input · invoices
amountloaded_atvendor_idinvoice_idinvoice_number
2502024-05-02 01:10:0071INV-1001
2502024-05-03 01:10:0072INV-1001
99.52024-05-02 02:00:0083INV-2001
Output
vendor_idinvoice_numberamountcopiesfirst_loaded_at
7INV-100125022024-05-02 01:10:00

Example 2no duplicates returns nothing

Input · invoices
amountloaded_atvendor_idinvoice_idinvoice_number
102024-05-01 00:00:0071INV-1
102024-05-01 00:00:0072INV-2
Output
no rows

Topics

deduplicationaggregation

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.sql
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.