Return different values based on conditions.
Section: Conditional, NULL, and Conversion Functions
Use CASE expression
sql
sql
SELECT order_id,
CASE
WHEN amount >= 1000 THEN 'large'
WHEN amount >= 100 THEN 'medium'
ELSE 'small'
END AS bucket
FROM orders;Explanation
CASE is one of the most useful SQL expressions for reporting and categorization.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Conditional, NULL, and Conversion Functions
Replace NULL with fallback value
Return the first non-NULL value in a list.
Convert a value to NULL conditionally
Return NULL when two expressions are equal.
Conditional aggregation with CASE
Compute multiple categorized totals in one query.