SQLSQL
#43

Top N Records Per Group Using PARTITION BY in SQL

MediumFlipkart
Interview Question #43
Asked at Flipkart

Given `products` (product_id, category, sales_volume), write a SQL query to select the top 3 products with the highest sales volume in each category.

Input Table: products
5 rows preview
product_idcategorysales_volume
1Laptops500
2Laptops450
3Laptops400
4Laptops300
5Phones900
Expected Output Structure4 rows
categoryproduct_idsales_volumecategory_rank
Laptops15001
Laptops24502
Laptops34003
Phones59001
Interview Context

Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.

SQLPostgreSQL 15
Ready to execute

Click "Run Query" or press Ctrl + Enter to test your query.

Output will be verified against the test dataset.

Chat with us