Top 3 Products per Category
SQL coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP
For each category, return the three products with the highest revenue.
Engine: queries run on MySQL 8.0, so use MySQL-compatible syntax and functions.
Tables
| column | type | description | | --- | --- | --- | | id | INT | identifies the product | | category | VARCHAR | the category the product belongs to | | product_name | VARCHAR | name of the product | | revenue | INT | total revenue for that product |
Return exactly these columns:
- category
- product_name
- revenue
A category with fewer than three products returns every product it has.
If two products in the same category have the same revenue, the one with the lower id ranks higher. Ties are tested, and the tie-break decides which products you select: a ranking that hands back a fourth row for a category where two products are level will not pass.
The grader compares your rows as a set, so the final ORDER BY is not itself graded - what is graded is which rows you return and how many. Sort by category ascending then revenue descending anyway, so your output lines up with the expected output while you work.
Example input
products
| id | category | product_name | revenue | | --- | --- | --- | --- | | 1 | Electronics | Laptop | 95000 | | 2 | Electronics | Phone | 72000 | | 3 | Electronics | Tablet | 45000 | | 4 | Electronics | Earbuds | 18000 | | 5 | Clothing | Jacket | 32000 | | 6 | Clothing | Jeans | 15000 | | 7 | Clothing | Shirt | 12000 | | 8 | Clothing | Shoes | 8000 | | 9 | Food | Coffee | 5000 | | 10 | Food | Tea | 3000 | | 11 | Food | Juice | 2500 | | 12 | Food | Water | 1000 |
Expected output
| category | product_name | revenue | | --- | --- | --- | | Clothing | Jacket | 32000 | | Clothing | Jeans | 15000 | | Clothing | Shirt | 12000 | | Electronics | Laptop | 95000 | | Electronics | Phone | 72000 | | Electronics | Tablet | 45000 | | Food | Coffee | 5000 | | Food | Tea | 3000 | | Food | Juice | 2500 |
Each category keeps its top three and drops the rest: Earbuds, Shoes and Water fall outside the top three of their categories. The later test cases add categories where two products share a revenue, which is where the tie-break matters.
What this SQL challenge teaches you
“Top 3 Products per Category” is a medium-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with window functions, RANK, partitioning — 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
- window functions
- RANK
- partitioning
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:
- You need a ranking that restarts for every category, which is what a window function's PARTITION BY does. Partition by category and order by revenue descending, so the highest-revenue product in each category is number one.
- You cannot filter on a window function in the WHERE of the same SELECT - the window is computed after WHERE runs. Compute the ranking in a subquery or a CTE, then filter on it from the outer query.
- Ranking functions disagree the moment two products in a category share a revenue. Ordering by revenue alone, with two products level at the top, ROW_NUMBER gives 1, 2, 3, 4 - RANK repeats then skips, 1, 1, 3, 4 - and DENSE_RANK repeats without skipping, 1, 1, 2, 3. Filtering on <= 3 then returns three rows from ROW_NUMBER but four from DENSE_RANK, and a fourth row is a wrong answer here.
- The fix is to make the ordering total rather than to pick a particular function: add id ASC as a second key inside OVER(...) so no two rows in a category tie any more. The lower id then ranks higher, as the problem requires, and RANK and DENSE_RANK produce the same correct result as ROW_NUMBER once the tie is broken.
- 'rank' is a reserved word in MySQL, so name the ranking column rn (or backtick it) rather than rank. Return exactly category, product_name and revenue.
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Flipkart, Walmart. 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
- Running Total Revenue
- Median Salary per Department
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
- 3-Day Rolling Sum Over Sparse Sales Data
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 medium and covers Window Functions.