CASE WHEN lets you perform conditional logic inside a SELECT statement, returning different values based on Boolean tests.
CASE WHEN evaluates Boolean conditions in order and returns the first matching result; if none match, it returns the ELSE value or NULL.
Use CASE WHEN for inline column-level decisions inside SELECT, UPDATE, and ORDER BY; use IF only in stored procedures or Snowflake Scripting blocks.
Start with CASE, add multiple WHEN condition THEN result pairs, finish with optional ELSE and mandatory END.
Yes.Wrap an inner CASE inside THEN to cascade logic, but keep nesting shallow for readability.
This query labels orders over $500 as ‘high’, between $100–$500 as ‘medium’, and the rest as ‘low’.
SELECT id,
total_amount,
CASE WHEN total_amount > 500 THEN 'high'
WHEN total_amount BETWEEN 100 AND 500 THEN 'medium'
ELSE 'low' END AS order_size
FROM Orders;
Place the most restrictive condition first; CASE stops searching after the first match, improving clarity and performance.
.
No, but omitting ELSE returns NULL when no condition matches, which can break NOT NULL columns.
Indirectly. Wrap CASE in a subquery or CTE, then filter on its result.
Minimal. Most overhead comes from the expressions inside WHEN, not from CASE itself.