CASE WHEN lets you create conditional logic inside SELECT, UPDATE, and ORDER BY statements in Amazon Redshift.
CASE WHEN evaluates conditions in order and returns the first matching result, allowing if-then-else logic directly in SQL queries.
Start with CASE
, list each WHEN condition THEN result
, add an optional ELSE default
, and finish with END
. Embed it in SELECT, UPDATE, or ORDER BY clauses.
Redshift supports searched CASE (multiple Boolean tests) and simple CASE (compare one expression to many values). Both can appear anywhere an expression is allowed.
Calculate customer tiers, flag low stock products, or group order amounts into ranges—all without extra joins or subqueries.
Order WHEN clauses from most to least likely, keep logic short, and always include an ELSE to avoid NULL surprises.
Redshift permits nesting, but limit depth to two levels for readability. Use CTEs if logic grows complex.
CASE runs per row; avoid heavy scalar functions inside WHEN clauses. Push filters to WHERE when possible.
No. Use CASE in SELECT, ORDER BY, or UPDATE. For conditional filtering, combine Boolean logic directly in WHERE.
Conceptually yes, but CASE is set-based and evaluates per row instead of per statement.
No. Stick with ANSI-standard CASE WHEN for maximum portability.