Lesson 20 / 30
CASE Expressions
Add if-else logic inside queries with CASE expressions and build conditional totals with SUM(CASE ...).
CASE WHEN
CASE picks a value based on conditions, evaluated top to bottom — like if/elif/else inside SQL.
SELECT name, amount,
CASE
WHEN amount > 1500 THEN 'High'
WHEN amount > 500 THEN 'Medium'
ELSE 'Low'
END AS tier
FROM orders;
Output:
name | amount | tier Asha Rao | 2000.00 | High Rahul Mehta | 1300.00 | Medium
CASE inside aggregates
SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) is a common pattern for conditional totals in one pass.