BigQuery · Conditional expression
BigQuery CASE
CASE returns the result of the first WHEN branch that matches. The simple form compares one value with a list (CASE status WHEN 'P' THEN 'pending' END); the searched form tests conditions (CASE WHEN amount >= 1000 THEN 'large' END). Use it to relabel, bucket or conditionally aggregate rows.
CASE is the general conditional expression in GoogleSQL. BigQuery checks the WHEN branches from top to bottom and returns the THEN value of the first one that matches; later branches are ignored. If nothing matches, CASE returns the ELSE value, or NULL when there is no ELSE.
The simple form, CASE expr WHEN value THEN ..., compares expr with each value using =. Because NULL = NULL is NULL rather than TRUE, a WHEN NULL branch never matches. The searched form, CASE WHEN condition THEN ..., accepts any BOOL condition, so it handles ranges, several columns and IS NULL checks.
All THEN and ELSE values must share a common supertype. INT64 and FLOAT64 mix (the result becomes FLOAT64), but a STRING in one branch and a number in another fails with No matching signature for operator CASE. Errors in branches that are not chosen are ignored, so CASE WHEN qty = 0 THEN NULL ELSE revenue / qty END never raises a division by zero error. For two branches, IF is shorter.
Syntax
CASE expr
WHEN expr_to_match THEN result
[ ... ]
[ ELSE else_result ]
END
CASE
WHEN condition THEN result
[ ... ]
[ ELSE else_result ]
ENDArguments and return value
| Argument | Description |
|---|---|
expr | Simple form only. The value compared with each expr_to_match. Any type that shares a supertype with the expr_to_match values. |
expr_to_match | Simple form only. A value compared with expr using =. The first equal one wins; a NULL here never matches. |
condition | Searched form only. A BOOL expression. The first condition that is TRUE wins; FALSE and NULL fall through to the next branch. |
result | The value returned for the matching branch. All result and else_result values must be coercible to one supertype. |
else_result | Optional. Returned when no branch matches. Without ELSE, CASE returns NULL. |
Returns: The supertype of all result and else_result values (for example FLOAT64 when branches mix INT64 and FLOAT64). NULL when no branch matches and there is no ELSE.
Examples
Relabel status codes with the simple form
WITH orders AS (
SELECT 1001 AS order_id, 'P' AS status_code UNION ALL
SELECT 1002, 'S' UNION ALL
SELECT 1003, 'R' UNION ALL
SELECT 1004, 'X'
)
SELECT
order_id,
CASE status_code
WHEN 'P' THEN 'pending'
WHEN 'S' THEN 'shipped'
WHEN 'R' THEN 'refunded'
ELSE 'unknown'
END AS status
FROM orders
ORDER BY order_id;| order_id | status |
|---|---|
| 1001 | pending |
| 1002 | shipped |
| 1003 | refunded |
| 1004 | unknown |
Bucket orders by amount
Branches are checked in order, so the widest range goes last. The NULL amount fails every comparison and falls through to ELSE.
WITH orders AS (
SELECT 1 AS order_id, 25.00 AS amount UNION ALL
SELECT 2, 180.00 UNION ALL
SELECT 3, 1200.00 UNION ALL
SELECT 4, NULL
)
SELECT
order_id,
amount,
CASE
WHEN amount >= 1000 THEN 'large'
WHEN amount >= 100 THEN 'medium'
WHEN amount >= 0 THEN 'small'
ELSE 'missing'
END AS order_size
FROM orders
ORDER BY order_id;| order_id | amount | order_size |
|---|---|---|
| 1 | 25.0 | small |
| 2 | 180.0 | medium |
| 3 | 1200.0 | large |
| 4 | NULL | missing |
WHEN NULL never matches
The simple form compares with =, so country = NULL is NULL for u2 and the ELSE branch returns the NULL country. The searched form with IS NULL catches it.
WITH users AS (
SELECT 'u1' AS user_id, 'DE' AS country UNION ALL
SELECT 'u2', NULL
)
SELECT
user_id,
CASE country WHEN NULL THEN 'no country' ELSE country END AS simple_form,
CASE WHEN country IS NULL THEN 'no country' ELSE country END AS searched_form
FROM users
ORDER BY user_id;| user_id | simple_form | searched_form |
|---|---|---|
| u1 | DE | DE |
| u2 | NULL | no country |
Conditional aggregation
SUM(CASE ...) and COUNT(CASE ...) turn status values into columns in one pass. COUNT skips the NULL that CASE returns when no branch matches and there is no ELSE.
WITH payments AS (
SELECT 'acme' AS customer, 'paid' AS status, 120 AS amount UNION ALL
SELECT 'acme', 'refunded', 20 UNION ALL
SELECT 'acme', 'paid', 75 UNION ALL
SELECT 'globex', 'paid', 300 UNION ALL
SELECT 'globex', 'failed', 50
)
SELECT
customer,
SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) AS paid,
SUM(CASE WHEN status = 'refunded' THEN amount ELSE 0 END) AS refunded,
COUNT(CASE WHEN status = 'failed' THEN 1 END) AS failed_payments
FROM payments
GROUP BY customer
ORDER BY customer;| customer | paid | refunded | failed_payments |
|---|---|---|---|
| acme | 195 | 20 | 0 |
| globex | 300 | 0 | 1 |
Common mistakes
- Testing for NULL with the simple form.
CASE country WHEN NULL THEN ...never matches becauseNULL = NULLis NULL. WriteCASE WHEN country IS NULL THEN ... END. - Putting a broad condition first. With
WHEN amount > 0aboveWHEN amount > 1000, every positive order lands in the first branch, because the first TRUE condition wins. - Mixing STRING and numeric results.
THEN 'high' ELSE 0fails with No matching signature for operator CASE, because BigQuery doesn't convert numbers to strings implicitly. Return one type from every branch, for exampleELSE CAST(score AS STRING). - Porting DECODE from Oracle or Snowflake. BigQuery has no DECODE function, so rewrite it as a simple CASE. DECODE treats two NULLs as equal and CASE does not, so add a
WHEN x IS NULLbranch where the original relied on that. - Expecting CASE to skip an expensive subquery. BigQuery ignores errors from branches it doesn't choose, but it may still evaluate them. Use the scripting
IF ... THENstatement when a subquery should only run under a condition.
Cost and performance
- Every column referenced in any branch is read from storage, even if that branch never matches, so on-demand queries are billed for those columns.
- Scalar subqueries inside CASE branches can run even when their branch is not chosen, because BigQuery doesn't guarantee lazy evaluation of conditional expressions. A subquery on a large table can add bytes scanned and slot time.
- For filtered aggregates, the GoogleSQL reference recommends the WHERE clause inside the aggregate call (Preview), such as
SUM(amount WHERE status = 'paid'), overSUM(CASE ...)for better performance.
FAQ
What is the difference between CASE and IF in BigQuery?
IF(cond, a, b) is shorthand for CASE WHEN cond THEN a ELSE b END. Use CASE when you have three or more branches or want to match one value against a list.
How do I use multiple conditions in one CASE WHEN?
Combine them with AND, OR and IN inside one WHEN: CASE WHEN country IN ('DE', 'AT') AND amount > 100 THEN 'dach_large' ELSE 'other' END. Each WHEN can test different columns.
Why does my CASE expression return NULL?
Either no branch matched and there is no ELSE, or the simple form compared a value with NULL. Add an ELSE branch, and use WHEN col IS NULL in the searched form to handle missing values.
Can I use CASE in WHERE, GROUP BY or ORDER BY?
Yes. CASE is an expression, so it works anywhere a value does. BigQuery also lets you GROUP BY or ORDER BY the alias you gave the CASE column in the SELECT list.
Related
Errors you may hit
All BigQuery conditional expressions · All functions · BigQuery error messages
