BigQuery · Conditional expression
BigQuery IF
IF(condition, true_result, else_result) returns true_result when the condition is TRUE and else_result when it is FALSE or NULL. Use it for two-way logic inside a query, such as flags and guarded calculations; switch to CASE for three or more branches.
IF is the two-branch conditional function in GoogleSQL: IF(amount >= 100, 'free', 'paid'). It is shorthand for CASE WHEN condition THEN true_result ELSE else_result END and works anywhere an expression does, including WHERE, GROUP BY, ORDER BY and inside aggregates.
The condition must be a BOOL. MySQL accepts a number (IF(1, ...)), but BigQuery fails with No matching signature for function IF, so compare explicitly: IF(flag = 1, ...). A NULL condition is not TRUE, so it returns else_result. Both results must share a supertype: IF(ok, 1, 2.5) returns FLOAT64 for every row, and IF(ok, 'yes', 0) fails.
BigQuery behaves as if only the chosen branch is evaluated, so errors in the other branch are ignored. That makes IF(clicks = 0, NULL, orders / clicks) a safe alternative to SAFE_DIVIDE. Snowflake calls this function IFF and SQL Server calls it IIF; neither name exists in BigQuery. The scripting statement IF ... THEN ... END IF; is a different thing: it decides which statements of a script run.
Syntax
IF(expr, true_result, else_result)Arguments and return value
| Argument | Description |
|---|---|
expr | A BOOL expression. TRUE returns true_result; FALSE or NULL returns else_result. Numbers are not accepted. |
true_result | The value returned when expr is TRUE. |
else_result | The value returned when expr is FALSE or NULL. Must share a supertype with true_result. |
Returns: The supertype of true_result and else_result.
Examples
Flag rows, including a NULL condition
NULL >= 100 is NULL, so order A-102 takes the else branch.
WITH orders AS (
SELECT 'A-100' AS order_id, 250 AS amount UNION ALL
SELECT 'A-101', 40 UNION ALL
SELECT 'A-102', NULL
)
SELECT
order_id,
amount,
IF(amount >= 100, 'free', 'paid') AS shipping
FROM orders
ORDER BY order_id;| order_id | amount | shipping |
|---|---|---|
| A-100 | 250 | free |
| A-101 | 40 | paid |
| A-102 | NULL | paid |
Guard a division
The division is never applied to the zero-click row, so the query doesn't fail.
WITH campaigns AS (
SELECT 'spring_sale' AS campaign, 1200 AS clicks, 36 AS orders UNION ALL
SELECT 'retargeting', 0, 0
)
SELECT
campaign,
IF(clicks = 0, NULL, orders / clicks) AS conversion_rate
FROM campaigns
ORDER BY campaign;| campaign | conversion_rate |
|---|---|
| retargeting | NULL |
| spring_sale | 0.03 |
Count and average conditionally
SUM(IF(..., 1, 0)) counts matching rows (COUNTIF does the same), and returning NULL keeps failed runs out of AVG.
WITH pipeline_runs AS (
SELECT 'orders_daily' AS pipeline, 'success' AS status, 312 AS duration_s UNION ALL
SELECT 'orders_daily', 'failed', 45 UNION ALL
SELECT 'users_sync', 'success', 128 UNION ALL
SELECT 'users_sync', 'success', 140
)
SELECT
pipeline,
SUM(IF(status = 'failed', 1, 0)) AS failed_runs,
AVG(IF(status = 'success', duration_s, NULL)) AS avg_success_s
FROM pipeline_runs
GROUP BY pipeline
ORDER BY pipeline;| pipeline | failed_runs | avg_success_s |
|---|---|---|
| orders_daily | 1 | 312.0 |
| users_sync | 0 | 134.0 |
Mixed branch types become FLOAT64
An INT64 branch and a FLOAT64 branch are coerced to their supertype, so the integer 1 comes back as 1.0.
SELECT
IF(TRUE, 1, 2.5) AS result,
TYPEOF(IF(TRUE, 1, 2.5)) AS result_type;| result | result_type |
|---|---|
| 1.0 | FLOAT64 |
Common mistakes
- Passing a number as the condition, as MySQL allows.
IF(1, 'a', 'b')fails with No matching signature for function IF; writeIF(is_active = 1, ...). - Forgetting that a NULL condition goes to the else branch.
IF(amount >= 100, 'free', 'paid')labels orders with a missing amount 'paid'. Checkamount IS NULLfirst if that matters. - Returning a STRING from one branch and a number from the other. BigQuery doesn't convert implicitly; cast one side, for example
IF(ok, 'yes', CAST(code AS STRING)). - Using IFF from Snowflake or IIF from SQL Server. Both fail with Function not found; BigQuery only has IF and CASE.
- Nesting IF three or four levels deep. It works, but a CASE with several WHEN branches is easier to read and change.
FAQ
Is there an IF statement in BigQuery?
Yes, in scripts: IF condition THEN ... ELSEIF condition THEN ... ELSE ... END IF; decides which statements run. Inside a query, use the IF function or CASE to compute a value per row.
What is the BigQuery equivalent of IFF or IIF?
IF. IFF(cond, a, b) from Snowflake and IIF(cond, a, b) from SQL Server translate directly to IF(cond, a, b).
How do I write IF with multiple conditions?
Combine the conditions with AND or OR: IF(country = 'DE' AND amount > 100, 'priority', 'standard'). For more than two outcomes, use CASE instead of nesting IFs.
Does IF evaluate both branches?
BigQuery behaves as if only the chosen branch is evaluated, so errors in the other branch are ignored. It may still compute the other branch internally, so don't rely on IF to skip an expensive subquery.
Related
Errors you may hit
All BigQuery conditional expressions · All functions · BigQuery error messages
