SQL Quest › SQL Interview Questions › Window Functions

Deduplicate Orders with ROW_NUMBER

HardProQuerying BasicsWindow FunctionsSubqueries & CTEs

Some customers placed multiple orders for the same product on the same date. Deduplicate by keeping only the order with the highest total for each (customer_id, product, order_date) combination. Show order_id, customer_id, product, order_date, and total. Sort by customer_id, order_date. This deduplication pattern with ROW_NUMBER is a top-5 FAANG SQL question.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".

Schema

orders

order_idcustomer_idproductcategoryquantitypricetotalorder_datecountrystatus
11Laptop ProElectronics11299.991299.992024-01-15USAcompleted
22Wireless MouseElectronics249.9999.982024-01-16Canadacompleted
33Office ChairFurniture1349.99349.992024-01-17USAcompleted

Expected output: Keep only the $999 order

Hint

This is a Pro challenge — the hint, the step-by-step tutor and the reference solution open in the app.

Concepts

SELECT Window Functions ROW_NUMBER PARTITION BY CTE Window Functions + CTE

Practise the topic: SQL practice questions · Window function practice · CTE practice · Ranking function practice · Advanced SQL interview questions

Read the concept: ROW_NUMBER vs RANK vs DENSE_RANK

In these company practice sets

Airbnb · Amazon · Anthropic · OpenAI · Plaid · Snowflake · Uber · Goldman Sachs

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Revenue Share by Category (Window %)Hard · ProRANK vs DENSE_RANK: Rating GapsHard · ProMedian Salary Without PERCENTILEHard · ProSliding Window Max RevenueHard · ProMonth-over-Month Revenue GrowthHard · ProDepartment Salary Percentile BucketsHard · Pro

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test