ROW_NUMBER: Remove Duplicate Rows and Keep One Per Order
SQL coding challenge · Difficulty: easy · Topic: Window Functions · +50 XP
Problem
A data pipeline bug created duplicate rows in the orders table. Clean it by keeping exactly one row per duplicate group.
Tables
Table: orders_dup
| order_id | customer_name | amount | | 1 | Krishna | 500 | | 1 | Krishna | 500 | | 2 | Parvati | 800 | | 2 | Parvati | 800 | | 3 | Atul | 300 |
Expected Output (deduplicated)
| order_id | customer_name | amount | | 1 | Krishna | 500 | | 2 | Parvati | 800 | | 3 | Atul | 300 |
- Return:
order_id,customer_name,amount - Keep only one row per duplicate group
- Sort by
order_idascending - Hint: Use
ROW_NUMBER() OVER (PARTITION BY order_id, customer_name, amount ORDER BY order_id)then filter wherern = 1
Success Criteria
- Your query returns three columns: order_id, customer_name, amount.
- Exactly one row survives from each group of identical rows.
- No row that was already unique is lost.
- The rows match the Expected Output exactly, with no extras.
What this SQL challenge teaches you
“ROW_NUMBER: Remove Duplicate Rows and Keep One Per Order” is a easy-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with ROW_NUMBER, PARTITION BY, Deduplication, Remove Duplicates — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- ROW_NUMBER
- PARTITION BY
- Deduplication
- Remove Duplicates
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- Duplicates cannot be removed with a WHERE clause alone, because every copy looks identical to it. You need a way to tell the copies apart first.
- A window function can number the rows inside each group: ROW_NUMBER() assigns 1, 2, 3... to rows that share the same values.
- PARTITION BY the columns that define a duplicate, then keep only the rows numbered 1. The numbering has to happen in a subquery or CTE -- WHERE runs before window functions are computed.
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Flipkart, Databricks, Google. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Top 3 Products per Category
- Running Total Revenue
- Median Salary per Department
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers Window Functions.