Request a demo

BigQuery · Conditional expression

BigQuery IFNULL

IFNULL(expr, null_result) returns expr, or null_result when expr is NULL. Use it to replace NULLs with a default such as 0 or 'unknown'. It is identical to COALESCE(expr, null_result) and is BigQuery's version of NVL and ISNULL.

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

IFNULL takes exactly two arguments. If the first is not NULL, BigQuery returns it; otherwise it returns the second. It is a synonym for COALESCE(expr, null_result), so pick whichever reads better. For three or more fallbacks, use COALESCE.

Both arguments must share a supertype. IFNULL(open_tickets, 0) works, and IFNULL(price, 0) on a NUMERIC column returns NUMERIC, but IFNULL(order_count, 'none') fails with No matching signature for function IFNULL because INT64 and STRING have no common type. Cast first: IFNULL(CAST(order_count AS STRING), 'none').

IFNULL only replaces NULL. Empty strings, zeros and placeholders like -1 pass through unchanged; turn them into NULL first with NULLIF. The most common pattern is IFNULL(SUM(o.amount), 0) after a LEFT JOIN, because SUM returns NULL for a group with no matching rows.

Syntax

IFNULL(expr, null_result)

Arguments and return value

ArgumentDescription
exprThe value returned when it is not NULL. Any type.
null_resultThe value returned when expr is NULL. Must share a supertype with expr. Errors in it are ignored when expr is not NULL.

Returns: The supertype of expr and null_result.

Examples

Default values for missing fields

WITH customers AS (
  SELECT 'c1' AS customer_id, 'Berlin' AS city, 3 AS open_tickets UNION ALL
  SELECT 'c2', NULL, NULL
)
SELECT
  customer_id,
  IFNULL(city, 'unknown') AS city,
  IFNULL(open_tickets, 0) AS open_tickets
FROM customers
ORDER BY customer_id;
Result
customer_idcityopen_tickets
c1Berlin3
c2unknown0

Zero instead of NULL after a LEFT JOIN

u2 has no orders, so SUM returns NULL for that group. IFNULL turns it into 0.

WITH users AS (
  SELECT 'u1' AS user_id UNION ALL
  SELECT 'u2' UNION ALL
  SELECT 'u3'
),
orders AS (
  SELECT 'u1' AS user_id, 50 AS amount UNION ALL
  SELECT 'u1', 70 UNION ALL
  SELECT 'u3', 20
)
SELECT
  u.user_id,
  IFNULL(SUM(o.amount), 0) AS lifetime_value
FROM users AS u
LEFT JOIN orders AS o USING (user_id)
GROUP BY u.user_id
ORDER BY u.user_id;
Result
user_idlifetime_value
u1120
u20
u320

Replacing NULL before AVG changes the answer

AVG skips NULLs. Replacing them with 0 counts the missing rating as a real zero and pulls the average from 4.5 down to 3.0.

WITH reviews AS (
  SELECT 'r1' AS review_id, 5 AS rating UNION ALL
  SELECT 'r2', 4 UNION ALL
  SELECT 'r3', NULL
)
SELECT
  AVG(rating) AS avg_rating,
  AVG(IFNULL(rating, 0)) AS avg_with_zeros
FROM reviews;
Result
avg_ratingavg_with_zeros
4.53.0

Common mistakes

  • Mixing types. IFNULL(quantity, 'n/a') fails when quantity is INT64; cast the column to STRING first, or keep it numeric and use a numeric default.
  • Expecting empty strings to be replaced. IFNULL('', 'unknown') returns the empty string. Use IFNULL(NULLIF(TRIM(city), ''), 'unknown').
  • Replacing NULLs inside AVG or MIN. AVG(IFNULL(rating, 0)) treats missing values as zeros; leave them NULL unless zero is the true value.
  • Looking for NVL or ISNULL. Both fail with Function not found in BigQuery; use IFNULL or COALESCE, and x IS NULL to test for NULL.

FAQ

What is the difference between IFNULL and COALESCE in BigQuery?

None for two arguments: IFNULL(a, b) is a synonym for COALESCE(a, b). COALESCE also accepts three or more arguments and returns the first non-NULL one.

What is the BigQuery equivalent of NVL or ISNULL?

IFNULL. Oracle's NVL(a, b) and SQL Server's ISNULL(a, b) become IFNULL(a, b) or COALESCE(a, b).

How do I replace NULL with 0 in BigQuery?

Use IFNULL(column, 0). For aggregates over outer joins, wrap the aggregate: IFNULL(SUM(amount), 0). COUNT never returns NULL, so it needs no wrapper.

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.