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.
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
| Argument | Description |
|---|---|
expr | The value returned when it is not NULL. Any type. |
null_result | The 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;| customer_id | city | open_tickets |
|---|---|---|
| c1 | Berlin | 3 |
| c2 | unknown | 0 |
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;| user_id | lifetime_value |
|---|---|
| u1 | 120 |
| u2 | 0 |
| u3 | 20 |
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;| avg_rating | avg_with_zeros |
|---|---|
| 4.5 | 3.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. UseIFNULL(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 NULLto 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.
Related
Errors you may hit
All BigQuery conditional expressions · All functions · BigQuery error messages
