COALESCE returns the first non-NULL value from a list of expressions, letting you replace NULLs with meaningful defaults in SELECT, WHERE, ORDER BY, and other clauses.
COALESCE returns the first non-NULL value from a comma-separated list of expressions. If every expression is NULL, it returns NULL. Because SQL evaluates expressions left to right, place the most likely non-NULL value first for efficiency.
COALESCE follows the ANSI SQL standard and works across PostgreSQL, MySQL, SQL Server, Snowflake, BigQuery, and more. ISNULL is vendor-specific. COALESCE also evaluates to the highest-precedence data type among its arguments, avoiding silent truncation.
SQL infers the result type by scanning the list until it finds the highest-precedence data type. All expressions are implicitly cast to that type. Mismatched types like INT and VARCHAR can cause implicit conversions that hurt performance, so align types explicitly.
COALESCE(expr1, expr2, …, exprN)
accepts two or more expressions. Each expression can be a column, literal, or subquery. At least one expression must be non-NULL to avoid a NULL result.
Use COALESCE to display fallbacks in reports: SELECT COALESCE(nickname, first_name, 'Guest') AS display_name FROM users;
The query shows nickname
when present, else first_name
, else the literal "Guest."
Yes. To filter rows where country
is either the stored value or the default 'US': WHERE COALESCE(country, 'US') = 'US'
. This avoids writing separate NULL checks.
Sort NULLs to the end by replacing them with high or low sentinel values: ORDER BY COALESCE(order_date, '9999-12-31')
. ANSI SQL uses NULLS LAST
, but COALESCE provides a portable hack.
COALESCE is shorthand for the most common CASE pattern: CASE WHEN expr1 IS NOT NULL THEN expr1 ELSE expr2 END
. Use CASE when you need complex boolean logic; use COALESCE for simple first-non-NULL logic.
Place indexed columns first to maximize sargability, cast literals to the same type as columns, and avoid wrapping indexed columns in COALESCE inside WHERE if possible. Instead, push COALESCE to the SELECT list or use computed columns.
The query SELECT id, COALESCE(local_revenue, usd_revenue, 0) AS revenue FROM sales;
returns local currency first, falls back to USD, then zero. Dashboards read cleaner, and warehouse models remain NULL-respecting.
Galaxy’s AI copilot autocompletes COALESCE patterns, warns when data types clash, and suggests indexed alternatives. Sharing a query in a Galaxy Collection lets teammates reuse the same NULL-handling logic without pasting SQL in Slack.
COALESCE is crucial because real-world tables contain NULLs that break aggregations, joins, and UI displays. Replacing NULLs consistently prevents misleading analytics and runtime errors. A portable, ANSI-compliant function, COALESCE enables the same query to run on PostgreSQL, MySQL, Snowflake, BigQuery, or SQL Server—reducing vendor lock-in. In modern data engineering pipelines, COALESCE simplifies transformation logic in dbt, Airflow, or Galaxy notebooks, making code more maintainable.
Minorly. Evaluation stops at the first non-NULL value, so keep the most common non-NULL expression first. Avoid using COALESCE on indexed columns in WHERE clauses.
Yes, but it is unnecessary. COALESCE already accepts multiple arguments. Nesting complicates readability without adding functionality.
Galaxy’s AI copilot autocompletes COALESCE patterns, checks data-type precedence, and highlights anti-patterns like wrapping indexed columns, speeding up development.
NVL predates the ANSI standard and supports only two arguments. COALESCE is standard-compliant and supports many arguments. Prefer COALESCE for portability.