BigQuery offers functions like IFNULL, COALESCE, NULLIF, and SAFE_ operators to replace, detect, or safeguard against NULL values during queries.
Use IFNULL(expression, replacement)
when you need a single fallback value. It evaluates the expression once and substitutes the replacement only if the result is NULL, keeping queries succinct and performant.
Apply COALESCE(col1, col2, col3, ...)
. BigQuery scans each argument in order and returns the first non-NULL. This is ideal for hierarchical data like primary, secondary, and tertiary contact emails.
Choose NULLIF(expr1, expr2)
to convert specific sentinel values (e.g., 0, empty strings) into NULL. This simplifies downstream aggregation logic by treating placeholders as true NULLs.
SELECT product_id, NULLIF(price, 0) AS price
turns zero prices into NULL, ensuring AVG(price) ignores them.
Functions like SAFE_DIVIDE(numerator, denominator)
return NULL instead of raising a division-by-zero error. They keep analytic pipelines from failing while still signaling invalid math.
Wrap metrics in FILTER (WHERE col IS NOT NULL)
or rely on BigQuery’s default NULL-ignoring behavior in AVG
, SUM
, and COUNT(col)
to avoid inflated counts.
Use IS NULL
and IS NOT NULL
for NULLs and =''
for empty strings. Combining both checks ensures complete data quality scans.
NULLIF
.IFNULL
or COALESCE
.SAFE_*
functions.Yes. COUNT(*) counts rows, while COUNT(column) ignores NULLs in that column.
For one expression both are similar, but COALESCE scales better for multiple fallbacks by avoiding nested IFNULL calls.
No. NULL handling is defined per query in BigQuery; use view layers or default values to enforce consistency.