Top 3 Products per Category

SQL coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP

Table: products

+--------------+---------+

| Column       | Type    |

+--------------+---------+

| id           | INT     |
| category     | VARCHAR |
| product_name | VARCHAR |
| revenue      | INT     |

+--------------+---------+

Problem

-------

For each category, return the top 3 products by

revenue (highest first). If two products tie on

revenue, rank the one with the lower id higher.

Return: category, product_name, revenue

Order: category ASC, then revenue DESC

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 |

+-------------+--------------+---------+

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

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:

  1. Your result should return: category, product_name, revenue,.
  2. Compare your output to the Expected Output — the columns, values and row order must match exactly.
  3. Use ROW_NUMBER() OVER (PARTITION BY category ORDER BY revenue DESC, id ASC) in a subquery, then keep the rows where the rank <= 3. Tip: 'rank' is a reserved word in MySQL — name the window column rn (or backtick it), not rank.

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

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.

Solve this challenge free on PySpark.in