NULL represents an unknown or missing value. Arithmetic or string operations that involve NULL propagate NULL and can ruin aggregates or comparisons. NULL functions let you interrogate or substitute these values so downstream logic behaves predictably.Standard SQL defines two portable NULL functions:• COALESCE – returns the first non-NULL argument in the list.• NULLIF – returns NULL if two expressions are equal, otherwise returns the first expression.Vendors add synonyms that accomplish similar tasks:• ISNULL (SQL Server, Sybase) – two-argument COALESCE.• IFNULL (MySQL, SQLite) – two-argument COALESCE.• NVL (Oracle, BigQuery) – two-argument COALESCE.Because COALESCE is ANSI-SQL compliant, it is the safest choice across databases. All functions evaluate arguments in order, stop at the first non-NULL candidate, and preserve data type precedence rules. NULLIF is often paired with COALESCE for defensive programming (e.g., COALESCE(NULLIF(col,''), 'N/A')). Watch out for performance impacts when large subqueries appear inside COALESCE arguments—each expression is still evaluated unless explicitly short-circuited by the optimizer.
expr1..N Any SQL expression
- returned in order of first non-NULLexpr1
- Any SQL expressionexpr2
- Any SQL expression to compare to expr1expr
- Any SQL expression to test for NULLCOALESCE, NULLIF, ISNULL, IFNULL, NVL, IS NULL, IS NOT NULL, CASE expression, DEFAULT keyword
SQL-92 (COALESCE and NULLIF)
COALESCE picks the first non-NULL value from its argument list. NULLIF compares two expressions and returns NULL when they are equal, otherwise the first expression. Use NULLIF to convert sentinel values into NULL, then COALESCE to substitute a default.
The resulting data type follows the database's type-precedence rules. If any argument is numeric and others are strings that can cast to numbers, the output may become numeric. Cast explicitly to keep the desired type.
Yes. A common pattern is COALESCE(NULLIF(col, ''), 'Unknown'). First NULLIF turns empty strings into NULL, then COALESCE supplies the placeholder.
COALESCE and NULLIF are portable. ISNULL, IFNULL, and NVL are not. For cross-vendor scripts rely on COALESCE/NULLIF or include dialect-specific branches.