← All SQL interview questions
Label each basket as Small, Medium or Large
EasyCASE StatementsMyntraMeesho
A merchandising team buckets shopping baskets by spend so it can size up its packaging inventory.
For every row in basket_summary, return basket_id, basket_value and a derived column named size_label:
Smallwhenbasket_valueis below 1000Mediumwhen it is at least 1000 but below 5000Largewhen it is 5000 or more
Sample data
Table: basket_summary
12 row(s) · showing first 8
| basket_idINT | customer_codeTEXT | item_countINT | basket_valueNUMERIC(10,2) |
|---|---|---|---|
| 9001 | CU-118 | 2 | 640.00 |
| 9002 | CU-224 | 5 | 1899.50 |
| 9003 | CU-317 | 1 | 999.99 |
| 9004 | CU-402 | 11 | 7420.00 |
| 9005 | CU-118 | 3 | 1000.00 |
| 9006 | CU-556 | 8 | 4999.95 |
| 9007 | CU-663 | 14 | 12500.00 |
| 9008 | CU-224 | 1 | 249.00 |
basket_summary
- basket_idINT
- customer_codeTEXT
- item_countINT
- basket_valueNUMERIC(10,2)
- Step 1: A `CASE` expression evaluates its `WHEN` branches top to bottom and stops at the first match, so the bucket order matters.
- Step 2: Put the lowest bucket first: `WHEN basket_value < 1000 THEN 'Small'`.
- Step 3: The next branch only sees rows that already failed the first test, so `WHEN basket_value < 5000 THEN 'Medium'` implicitly means 1000–4999.99.
- Step 4: Everything remaining is 5000 or more, so an `ELSE 'Large'` closes the expression.
- Step 5: Alias the expression as `size_label` and select it alongside `basket_id` and `basket_value`.
SELECT basket_id,
basket_value,
CASE
WHEN basket_value < 1000 THEN 'Small'
WHEN basket_value < 5000 THEN 'Medium'
ELSE 'Large'
END AS size_label
FROM basket_summary;Learn the concept first
Stuck on CASE Statements? These free SQL lessons teach it step by step.
More CASE Statements questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program