Ravindra BagaleCourses & study guides Track your progress

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

  1. Order size buckets.
  2. Priority custom sort.
  3. 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_idamount_inrsize
10161780.0Large
10131680.0Large
10071080.0Large
10041050.0Large
1010930.0Medium
1019900.0Medium

Meaning. Top amounts with size labels.

Ravindra Bagale's Tip

Keep ELSE so no row falls through with a blank label.