Request a demo

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.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

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

ArgumentDescription
exprA BOOL expression. TRUE returns true_result; FALSE or NULL returns else_result. Numbers are not accepted.
true_resultThe value returned when expr is TRUE.
else_resultThe 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;
Result
order_idamountshipping
A-100250free
A-10140paid
A-102NULLpaid

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;
Result
campaignconversion_rate
retargetingNULL
spring_sale0.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;
Result
pipelinefailed_runsavg_success_s
orders_daily1312.0
users_sync0134.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
resultresult_type
1.0FLOAT64

Common mistakes

  • Passing a number as the condition, as MySQL allows. IF(1, 'a', 'b') fails with No matching signature for function IF; write IF(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'. Check amount IS NULL first 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.

Errors you may hit

All BigQuery conditional expressions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.