For each category, find the products with the top 3 *distinct* net revenue values. Net revenue = SUM(qty * unit_price) over order lines that were NOT returned (`is_returned = 0`). Products with no non-returned sales are ignored. Products with equal revenue share a rank and the next rank is not skipped (so a category can return more than 3 products). Return `category`, `product_name`, `net_revenue` (ROUND 2), `rnk`. Order by `category`, `rnk`, `product_name`.
| Column | Type |
|---|---|
| product_id | INTEGER |
| product_name | TEXT |
| category | TEXT |
| product_id | product_name | category |
|---|---|---|
| 1 | Echo Mini | Electronics |
| 2 | Kindle Lite | Electronics |
| 3 | Fire Stick | Electronics |
| 4 | Smart Plug | Electronics |
| 5 | Yoga Mat | Fitness |
| 6 | Dumbbell Set | Fitness |
| 7 | Resistance Band | Fitness |
| Column | Type |
|---|---|
| order_id | INTEGER |
| product_id | INTEGER |
| qty | INTEGER |
| unit_price | REAL |
| is_returned | BOOLEAN |
| order_id | product_id | qty | unit_price | is_returned |
|---|---|---|---|---|
| 1 | 1 | 2 | 40 | 0 |
| 2 | 2 | 1 | 90 | 0 |
| 3 | 3 | 3 | 30 | 0 |
| 4 | 4 | 4 | 15 | 0 |
| 5 | 1 | 1 | 40 | 1 |
| 6 | 5 | 2 | 25 | 0 |
| 7 | 6 | 1 | 60 | 0 |
| 8 | 7 | 5 | 8 | 0 |
| category | product_name | net_revenue | rnk |
|---|---|---|---|
| Electronics | Fire Stick | 90 | 1 |
| Electronics | Kindle Lite | 90 | 1 |
| Electronics | Echo Mini | 80 | 2 |
| Electronics | Smart Plug | 75 | 3 |
| Fitness | Dumbbell Set | 60 | 1 |
| Fitness | Yoga Mat | 50 | 2 |
| Fitness | Resistance Band | 40 | 3 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Prime Next-Month Cohort Retention · Frequently Bought Together Pairs ›
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.