SQL Functions and Aggregation/Use CASE expression

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.
OpenIn sheetsqlsame section
Convert a value to NULL conditionally
Return NULL when two expressions are equal.
OpenIn sheetsqlsame section
Convert data types
Cast an expression to a different type.
OpenIn sheetsqlsame section
Conditional aggregation with CASE
Compute multiple categorized totals in one query.
OpenIn sheetsql1 tag match
Concatenate strings
Join multiple strings into one value.
Get current date and timestamp
Return the current date and current timestamp.