COALESCE SQL Function
COALESCE() is one of the handiest functions in SQL. Read this tutorial to learn how to master it.
What is the COALESCE()function?
COALESCE()function?Coalesce returns the first non-null value in a list. If all the values in the list are NULL, then the function returns null.
When to use COALESCE()?
COALESCE()?This function is useful when combining the values from several columns into one.
For example, a table called users contains values of users' work_email and personal_email.
Using the COALESCE() function, we can create a column called email, which shows the user's work_email if it is not null. Otherwise, it shows personal_email.
user_id | work_email | personal_email | COALESCE(work_email, personal_email) |
|---|---|---|---|
| 1 | [email protected] | null | [email protected] |
| 2 | null | [email protected] | [email protected] |
| 3 | [email protected] | [email protected] | [email protected] |
COALESCE() syntax
COALESCE() syntaxCOALESCE(value_1, value_2, ...., value_n)
The COALESCE() function takes in at least one value (value_1). It will return the first value in the list that is non-null.
For example, it will first check if value_1 is null. If not, then it returns value_1. Otherwise, it checks if value_2 is null. The process goes on until the list is complete.
COALESCE() examples
COALESCE() examplesExample 1: COALESCE() a column with a constant
COALESCE() a column with a constantConsider the table countries with a list of countries and their national days. Some national days are empty. For rows with empty national days, we can fill it in with the value 'Unknown'.
The query and the result are as follows.
SELECT
country_id,
name,
national_day,
COALESCE(national_day, 'Unknown') AS national_day_coalesced
FROM countries
ORDER BY country_idNotice how the null value in national_day is replaced by a constant Unknown.
Example 2: COALESCE() two columns
COALESCE() two columnsWe have a table named products. It contains the product name and its description. Some descriptions are too long (more than 60 characters). In that case, we replace the description with the product name.
The query and the result are as follows.
SELECT DISTINCT
product_name,
description,
COALESCE(
CASE WHEN
LENGTH(description) >= 60
THEN NULL
ELSE description
END,
product_name) product_name_or_description
FROM productsNotice how the column product_name_or_description displays the product_name if the description is long. Otherwise, it displays the description.
Example 3: COALESCE() three columns or more
COALESCE() three columns or moreWe can take example 2 one step further. Assume that there are currently two requirements.
- If the length of the
descriptionis less than 60, then display thedescription. - Otherwise, check if the length of the
product_nameis less than 20. If so, we display theproduct_name - Otherwise, display
product
SELECT DISTINCT
product_name,
description,
COALESCE(
CASE
WHEN LENGTH(description) > 50
THEN NULL
ELSE description
END,
CASE
WHEN LENGTH(product_name) > 14
THEN NULL
ELSE product_name
END,
'product') AS product_name_or_description
FROM products
ORDER BY product_nameNotice how the column product_name_or_description displays either the product_name or the description depending on the lengths of the product_name or description.
Technical requirements
COALESCE() works in SQL Server (starting with 2008), Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse, BigQuery, and Amazon RedShift.