Detect Possible Duplicate Vendor Invoices With SQL
Last updated: November 29, 2026
Quick Overview
Given an accounts payable table of invoices, write a SQL query that returns pairs of invoices from the same vendor with the same amount whose invoice dates are within 30 days of each other and whose invoice numbers match after normalization.
EY
November 29, 20260
0
0 solved
Given an accounts payable table of invoices, write a SQL query that returns pairs of invoices from the same vendor with the same amount whose invoice dates are within 30 days of each other and whose invoice numbers match after normalization.
Duplicate payment testing is a standard analytic in audit and forensic engagements, and data engineers at EY build these tests to run across many clients' payables data. The interviewer checks self-joins, date arithmetic and whether you avoid reporting each pair twice.
What the Interviewer Expects
- Self-join on vendor and amount
- Normalize invoice numbers (case, spaces, leading zeros, punctuation)
- Constrain to a 30 day window with correct date arithmetic
- Return each pair once using an id ordering condition
- Discuss false positives such as recurring monthly charges
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you catch near duplicates where the amount differs by a few cents or digits are transposed?
- How would you make this efficient on 50 million invoices?
- How would you exclude credit notes and reversed invoices?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Table ap_invoices(invoice_id, vendor_id, invoice_number, invoice_date, amount, status). Assume one row per invoice, amounts in a single currency, and that cancelled invoices have status = 'VOID'. ...
Approach
Build a cleaned CTE that uppercases the invoice number, strips non-alphanumerics and leading zeros. Self-join on vendor, amount and normalized number with a.invoice_id < b.invoice_id so each pair ap...