Request a demo

BigQuery · Conditional expression

BigQuery COALESCE

COALESCE(expr1, expr2, ...) returns the first argument that is not NULL, or NULL if every argument is NULL. Use it for fallback chains such as COALESCE(updated_at, created_at) and to merge key columns after a FULL OUTER JOIN.

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

COALESCE checks its arguments from left to right and returns the first non-NULL value. With two arguments it behaves exactly like IFNULL; with more, it builds a fallback chain: COALESCE(billing_email, account_email, 'no email on file').

All arguments must be coercible to one supertype. INT64 and FLOAT64 mix (the result is FLOAT64), but a STRING fallback for an INT64 column fails with No matching signature for function COALESCE. Only NULL is skipped: an empty string or 0 is a value and wins. Wrap such placeholders in NULLIF when they mean missing.

Arguments after the first non-NULL one are treated as not evaluated, so a fallback that would fail, such as 1 / 0, raises no error when an earlier argument has a value. BigQuery doesn't promise lazy execution, though: scalar subqueries passed as fallbacks can still run and scan their tables.

Syntax

COALESCE(expr[, ...])

Arguments and return value

ArgumentDescription
exprOne or more values of any type that share a supertype. Checked from left to right.

Returns: The supertype of all arguments: the first non-NULL value, or NULL if all arguments are NULL.

Examples

Last activity from two timestamps

Tickets that were never updated fall back to their creation time.

WITH tickets AS (
  SELECT 101 AS ticket_id, TIMESTAMP '2026-11-20 09:15:00 UTC' AS created_at, TIMESTAMP '2026-11-21 16:40:00 UTC' AS updated_at UNION ALL
  SELECT 102, TIMESTAMP '2026-11-22 11:05:00 UTC', NULL
)
SELECT
  ticket_id,
  COALESCE(updated_at, created_at) AS last_activity
FROM tickets
ORDER BY ticket_id;
Result
ticket_idlast_activity
1012026-11-21 16:40:00 UTC
1022026-11-22 11:05:00 UTC

Fallback chain with a final default

WITH customers AS (
  SELECT 'c1' AS customer_id, 'billing@acme.example' AS billing_email, 'ops@acme.example' AS account_email UNION ALL
  SELECT 'c2', NULL, 'finance@globex.example' UNION ALL
  SELECT 'c3', NULL, NULL
)
SELECT
  customer_id,
  COALESCE(billing_email, account_email, 'no email on file') AS send_invoice_to
FROM customers
ORDER BY customer_id;
Result
customer_idsend_invoice_to
c1billing@acme.example
c2finance@globex.example
c3no email on file

Merge keys after a FULL OUTER JOIN

Each side of a full join is NULL where the other side has no match. COALESCE takes the key from whichever side exists and turns missing counts into 0.

WITH warehouse AS (
  SELECT 'SKU-1' AS sku, 40 AS in_stock UNION ALL
  SELECT 'SKU-2', 15
),
storefront AS (
  SELECT 'SKU-2' AS sku, 12 AS listed UNION ALL
  SELECT 'SKU-3', 8
)
SELECT
  COALESCE(w.sku, s.sku) AS sku,
  COALESCE(w.in_stock, 0) AS in_stock,
  COALESCE(s.listed, 0) AS listed
FROM warehouse AS w
FULL OUTER JOIN storefront AS s
  ON w.sku = s.sku
ORDER BY sku;
Result
skuin_stocklisted
SKU-1400
SKU-21512
SKU-308

Empty strings are not NULL

COALESCE keeps the empty company name for l1. NULLIF turns '' into NULL first, so the fallback applies.

WITH leads AS (
  SELECT 'l1' AS lead_id, '' AS company, 'Acme GmbH' AS company_from_domain UNION ALL
  SELECT 'l2', NULL, 'Globex Ltd'
)
SELECT
  lead_id,
  COALESCE(company, company_from_domain) AS company_raw,
  COALESCE(NULLIF(company, ''), company_from_domain) AS company_clean
FROM leads
ORDER BY lead_id;
Result
lead_idcompany_rawcompany_clean
l1Acme GmbH
l2Globex LtdGlobex Ltd

Common mistakes

  • Mixing types. COALESCE(user_id, 'anonymous') fails when user_id is INT64; use COALESCE(CAST(user_id AS STRING), 'anonymous').
  • Expecting '' or 0 to be skipped. COALESCE only skips NULL; write COALESCE(NULLIF(company, ''), fallback).
  • Expecting NULL keys to match in a join. ON a.region = b.region never matches two NULLs. If they should match, join on COALESCE(a.region, '') = COALESCE(b.region, '') and make sure '' is not a real value.
  • Using COALESCE to pick the larger or smaller value. It returns the first non-NULL argument, not the greatest; use GREATEST or LEAST.

Cost and performance

  • Scalar subqueries used as fallbacks, as in COALESCE((SELECT ...), (SELECT ...)), are not guaranteed to be skipped when an earlier argument is non-NULL, so each can scan its table and add to bytes billed. In a script, check the first value and run the fallback query only when it is needed.

FAQ

What is the difference between COALESCE and IFNULL in BigQuery?

For two arguments they are the same: IFNULL(a, b) is a synonym for COALESCE(a, b). COALESCE accepts any number of arguments; IFNULL accepts exactly two.

Why does COALESCE fail with No matching signature?

The arguments have no common type, typically a STRING default for a numeric or date column. Cast the column, for example CAST(x AS STRING), or use a default of the same type.

How do I treat empty strings as NULL in COALESCE?

Wrap the column in NULLIF: COALESCE(NULLIF(TRIM(name), ''), 'unknown'). TRIM also catches values that contain only spaces.

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.