SQL COALESCE returns the first non-NULL value from a list of expressions. It’s a portable, ANSI-standard way to swap NULLs for defaults, build safe concatenations, and simplify CASE logic across PostgreSQL, MySQL, SQL Server, BigQuery, and more.
SQL COALESCE scans its arguments from left to right and returns the first value that is not NULL. If every argument is NULL, the function itself yields NULL.
COALESCE condenses multi-line CASE statements into a single, readable call. It also follows the ANSI SQL standard, making your query portable across databases without modification.
The syntax is straightforward: COALESCE(expr1, expr2, …)
. Provide two or more expressions of compatible data types, separated by commas.
All expressions must share a type precedence relationship. Most engines let you mix numeric types or character types, but avoid combining text with dates unless you CAST explicitly.
Use COALESCE to fall back to a literal when a column is NULL. This is common for reporting totals, showing placeholder text, or safeguarding arithmetic divisions.
Concatenation operators often propagate NULL, causing entire strings to vanish. Wrapping each nullable column in COALESCE substitutes an empty string, preserving the final output.
Yes. While IFNULL (MySQL) and ISNULL (SQL Server) accept only two arguments, COALESCE scales to any number, reducing nesting and improving clarity.
Most databases evaluate arguments in order and stop at the first non-NULL value, similar to short-circuit logic. This can save computation on expensive expressions that appear later.
Wrapping numeric columns with COALESCE prevents NULL from propagating through sums, averages, or divisions, ensuring calculations return 0 or another sensible default.
Using COALESCE on an indexed column can inhibit index seeks because it transforms the value. Consider computed columns or WHERE clauses that reference the raw column plus OR
logic.
Common scenarios include customer names with optional middle names, order totals missing discounts, date ranges with open ends, and CSV exports that need tidy placeholders.
Yes. Wrapping each input to SUM()
or AVG()
with COALESCE avoids NULL chaos without altering the aggregate’s logic.
COALESCE lets you merge incoming values with existing rows, choosing the new value when provided, or persisting the old value when the input is NULL.
COALESCE appears in the SQL-92 standard, so it works in PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, Snowflake, BigQuery, and more without vendor-specific tweaks.
COALESCE introduces minimal overhead. The main cost stems from lost index usage if applied in WHERE clauses. Evaluate execution plans and add computed indexes when needed.
Look for Compute Scalar or Project nodes that evaluate the function. Confirm that scans stop once a non-NULL value appears, ensuring no redundant computation.
Yes. You can wrap a windowed result—such as MAX() OVER()
—with COALESCE to default missing values in analytic queries.
NVL accepts only two parameters and performs implicit type conversion. COALESCE supports multiple parameters and honors standard type precedence rules.
Boolean columns can be wrapped in COALESCE to replace NULL with TRUE or FALSE, streamlining filter predicates where three-valued logic complicates comparisons.
Limit the argument list to essential expressions, match data types explicitly, place cheapest expressions first for performance, and document defaults to aid future maintainers.
COALESCE is a concise, portable, and powerful function for taming NULLs. Use it to supply defaults, secure calculations, and simplify conditional logic while respecting data types and index usage.
ISNULL is vendor-specific and accepts only two arguments. COALESCE follows the ANSI standard and handles any number of expressions, making it more portable.
COALESCE returns NULL when no argument provides a value. Plan downstream logic to handle that possibility.
Yes. COALESCE evaluates per row before grouping, letting you bucket NULLs under a default value like ‘Unknown’.
Function overhead is negligible, but misuse on indexed columns can trigger scans. Check execution plans if performance shifts.