Lesson 20 / 28

CASE Expressions

Inline if/else logic inside a query.

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.