COALESCE returns the first non-NULL value from a list of expressions, letting you replace NULLs with defaults in a portable, ANSI-SQL-compliant way.
The COALESCE function returns the first non-NULL expression in its argument list, letting you replace NULLs with meaningful default values.
COALESCE scans its arguments from left to right and returns the first one that is not NULL. If every argument is NULL the result is NULL. Use it anywhere you would use a scalar expression, including SELECT, WHERE, GROUP BY, HAVING, and ORDER BY.
Database optimizers rewrite COALESCE(a, b, c) as CASE WHEN a IS NOT NULL THEN a WHEN b IS NOT NULL THEN b ELSE c END. The evaluation stops at the first non-NULL expression, giving COALESCE short-circuit semantics similar to logical OR.
Choose COALESCE for cross-platform SQL. Vendor-specific ISNULL (SQL Server) and NVL (Oracle) accept only two arguments and break portability. Use them only when you need their special type-casting behavior on a single platform.
Yes. COALESCE supports unlimited arguments, so you can cascade through many columns or literals. The database stops evaluation once it finds a non-NULL value, saving compute time on expensive functions farther right.
Return a user’s primary email, falling back to secondary_email, then a placeholder.SELECT
user_id,
COALESCE(email, secondary_email, 'no_email@example.com') AS primary_email
FROM users;
Substitute a promotional price, then the regular price, then zero if both are NULL.SELECT
product_id,
COALESCE(promo_price, regular_price, 0) AS final_price
FROM products;
Place the most selective, non-NULL-prone column first to minimize evaluation cost. Match data types across arguments to avoid implicit casts. Document default literals clearly so downstream consumers know where values originate.
COALESCE can hide legitimate NULLs, masking data-quality issues. Audit data before blanket replacement. Also, implicit casting can slow queries; explicitly CAST when mixing types. Finally, avoid COALESCE in filtered indexes because it forces function evaluation on every row.
Galaxy’s AI copilot autocompletes COALESCE syntax, suggests fallback literals based on column stats, and warns when argument types differ. You can share the query in a Galaxy Collection so teammates reuse the same NULL-handling logic without copy-pasting SQL elsewhere.
COALESCE is ANSI-SQL, supports many arguments, short-circuits evaluation, and is ideal for replacing NULLs. Use it thoughtfully to balance data quality, performance, and readability.
COALESCE simplifies NULL handling, a constant pain point in analytics. Replacing NULLs early prevents downstream joins, aggregations, and visualizations from breaking. Because COALESCE is ANSI-SQL, teams can write one query that runs on PostgreSQL, Snowflake, BigQuery, and SQL Server, reducing vendor lock-in.
Because the optimizer rewrites COALESCE into a CASE expression, performance is usually identical. COALESCE can be marginally faster in some engines due to simpler syntax parsing, but differences are negligible.
Yes. Each argument can be a scalar subquery as long as it returns a single value per row. Keep subqueries inexpensive because COALESCE may need to evaluate them if earlier arguments are NULL.
Galaxy’s AI copilot autocompletes the function, suggests default values, and highlights type mismatches. You can also endorse a COALESCE-based query in a Collection so your team reuses the same NULL-handling logic.
Yes. COALESCE is defined in the SQL-92 standard and is supported by all major relational databases, making it a safe choice for portable code.