COALESCE is a scalar function defined in the SQL standard that scans its arguments from left to right and returns the first one that is not NULL. If all arguments are NULL, the function itself evaluates to NULL.Because COALESCE follows the same type-coercion rules as CASE, the result data type is the one with the highest precedence among the supplied expressions. This means you can mix numeric and character types, but the final data type will be promoted according to the database’s precedence rules.COALESCE is often used to provide default values, clean up NULLs in reports, or simplify CASE statements. It is deterministic, side-effect free, and can be nested or combined with other functions. All arguments are evaluated in most databases, so avoid heavy subqueries in later positions if performance matters.In distributed query engines, COALESCE behaves the same way but be mindful of NULL semantics across joined or unioned datasets.
• expression1, expression2, … expressionN
- Any scalar expression. The function requires at least two arguments and supports an arbitrary number. Each expression can be of any data type that can be implicitly converted to a common type.SQL-92 standard
It returns the data type with the highest precedence among its arguments, following the database’s implicit conversion rules.
Functionally they are similar. COALESCE is more concise but performance is usually the same because many optimizers rewrite it as a CASE expression.
Indexes are applied to the underlying columns, not the function output. However, you can create a computed column using COALESCE and index that computed column in databases that support it.
Use COALESCE(date_column, CURRENT_DATE). The function will return date_column when it is not NULL or the current date when it is NULL.