8. CASE WHEN: If-Then Logic in SQL
8.5 Practice: Ticket Priority and Order Size Labels
Why. Labels on tickets and order sizes show CASE in real stories.
What. Practice CASE on interview_mini and raja_coffee.
How. Write rules → CASE → optional GROUP BY counts.
Steps
- Order size buckets.
- Priority custom sort.
- Explain one row’s label out loud.
Examples
Example 1 · dataset: raja_coffee
SELECT order_id, amount_inr,
CASE WHEN amount_inr >= 1000 THEN 'Large'
WHEN amount_inr >= 600 THEN 'Medium'
ELSE 'Small' END AS size
FROM orders
ORDER BY amount_inr DESC
LIMIT 6;Output
| order_id | amount_inr | size |
|---|---|---|
| 1016 | 1780.0 | Large |
| 1013 | 1680.0 | Large |
| 1007 | 1080.0 | Large |
| 1004 | 1050.0 | Large |
| 1010 | 930.0 | Medium |
| 1019 | 900.0 | Medium |
Meaning. Top amounts with size labels.
Ravindra Bagale's Tip
Keep ELSE so no row falls through with a blank label.
Ravindra Bagale's Tip – मराठी
ELSE ठेवा — कोणताही रो रिकाम्या लेबलवर अडकू नये.
Ravindra Bagale's Tip – हिंदी
ELSE रखें — कोई पंक्ति खाली लेबल पर न छूटे।