Back to Library

COALESCE() in SQL

Returns the first non-null value in a list of expressions, evaluated from left to right.

Syntax

COALESCE(expression1, expression2 [, expression3, ...])
Return type
Data type of first non-null expression

COALESCE() Function Example

SELECT COALESCE(email, phone, 'No Contact') AS contact_info
FROM customers;
-- Output: 'john@email.com' (first non-null value)

What is COALESCE() in SQL?

The COALESCE() function in SQL returns the first non-null value from a list of expressions, evaluated from left to right. If all expressions are NULL, it returns NULL. This function is useful for handling missing data, providing default values, and avoiding null-related errors.

COALESCE() is ANSI SQL-compliant and is supported in SQL Server, MySQL, PostgreSQL, and Oracle.

The COALESCE() function is widely used in data cleaning, reporting, and ensuring completeness in SQL queries.

Parameters:

  • expression1, expression2, ...: List of expressions to evaluate

  • Minimum of two expressions required

Example Use Cases:

-- Default value handling

SELECT product_name,
       COALESCE(discount_price, regular_price) AS final_price
FROM products;
-- Output:
product_name        | final_price
--------------------|-------------
Laptop Dell XPS     | 899.99
iPhone 13           | 699.99
Samsung TV          | 1299.99
Nike Running Shoes  | 79.99
Coffee Maker        | 49.99
Wireless Earbuds    | 129.99

-- Contact preference

SELECT customer_name,
       COALESCE(mobile, home_phone, work_phone, 'No Phone') AS contact_number
FROM customers;
-- Output:
customer_name    | contact_number
-----------------|----------------
John Smith       | 555-0123
Maria Garcia     | 555-4567
James Brown      | 555-8901
Sarah Wilson     | 555-2345
Michael Lee      | No Phone
Emily Davis      | 555-6789
David Martinez   | 555-3456

-- Report formatting

SELECT employee_name,
       COALESCE(department, division, 'Unassigned') AS business_unit
FROM employees;
-- Output:
employee_name    | business_unit
-----------------|---------------
John Smith       | Sales
Maria Garcia     | Marketing
James Brown      | Engineering
Sarah Wilson     | Finance
Michael Lee      | Operations
Emily Davis      | HR
David Martinez   | Unassigned

Notes:

  • Behavior: Returns NULL only if all arguments are NULL

  • Performance Considerations: Evaluates arguments in order until non-null found

  • Version Info: Core SQL feature available in all DBMS

  • Deprecated/Recommended Alternatives: None, standard functionality

Error Handling:

  • Error: Type mismatch between expressions

  • Recommendation: Ensure consistent data types across expressions


Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

COALESCE()

COALESCE(col1, col2, 'default')

Returns first non-null

PostgreSQL

COALESCE()

COALESCE(col1, col2, 'default')

Returns first non-null

SQL Server

COALESCE()

COALESCE(col1, col2, 'default')

Returns first non-null

SQLite

COALESCE()

COALESCE(col1, col2, 'default')

Returns first non-null

BigQuery

COALESCE()

COALESCE(col1, col2, 'default')

Returns first non-null

Snowflake

COALESCE()

COALESCE(col1, col2, 'default')

Returns first non-null

Athena

COALESCE()

COALESCE(col1, col2, 'default')

Returns first non-null

Related Functions:

CAST/CONVERT()

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

COALESCE()

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

LAST_VALUE()

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

FIRST_VALUE()

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

LEAD()

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

LAG()

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

DENSE_RANK()

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

RANK()

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

ROW_NUMBER()

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

One place for all your queries,
directly on your SQL editor