← All SQL interview questions
Price spread across shelf categories
EasyAggregate FunctionsWalmartTarget
A category manager wants to see how wide the price range is in each aisle before planning a pricing review.
From shelf_prices, return for every category: item_count, min_price, max_price, avg_price (rounded to 2 decimals) and price_range (maximum minus minimum). Show the category with the widest range first and break ties alphabetically by category.
Sample data
Table: shelf_prices
14 row(s) · showing first 8
| skuINT | product_nameTEXT | categoryTEXT | priceNUMERIC(8,2) |
|---|---|---|---|
| 1 | Full Cream Milk 1L | Dairy | 68.00 |
| 2 | Paneer 200g | Dairy | 95.00 |
| 3 | Greek Yogurt | Dairy | 120.00 |
| 4 | Butter 500g | Dairy | 285.00 |
| 5 | Potato Chips | Snacks | 20.00 |
| 6 | Namkeen Mix | Snacks | 55.00 |
| 7 | Choco Cookies | Snacks | 40.00 |
| 8 | Protein Bar | Snacks | 150.00 |
shelf_prices
- skuINT
- product_nameTEXT
- categoryTEXT
- priceNUMERIC(8,2)
- Step 1: Group by `category` so every aggregate is calculated per aisle.
- Step 2: `MIN`, `MAX`, `AVG` and `COUNT` can all be used in the same `SELECT` list.
- Step 3: You can compute the range directly as `MAX(price) - MIN(price)`; a column alias cannot be reused inside the same `SELECT`, but it can be used in `ORDER BY`.
SELECT category,
COUNT(*) AS item_count,
MIN(price) AS min_price,
MAX(price) AS max_price,
ROUND(AVG(price), 2) AS avg_price,
MAX(price) - MIN(price) AS price_range
FROM shelf_prices
GROUP BY category
ORDER BY price_range DESC, category;More Aggregate Functions questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program