How to use SQL COALESCE() to determine non-NULL values

The SQL COALESCE() function is used to determine the first value within a list that is not NULL. The function is therefore a shortened version of a corresponding CASE statement.

What is SQL COALESCE()?

In SQL, the keyword NULL represents data that has no value. This situation arises, for example, when a specific value in a table is unknown and the corresponding field is left empty. This doesn’t necessarily mean the field has no value, but rather that the value is unknown. While NULL values aren’t inherently problematic, they can cause confusion if they accumulate in a database. The SQL COALESCE() function helps by outputting the first value in a list that is not NULL.

VPS Hosting
Fully virtualised servers with root access
  • Unlimited traffic and up to 1 Gbit/s bandwidth
  • Fast SSD NVMe storage
  • Free Plesk Web Host Edition

Syntax and requirements

The syntax of SQL COALESCE() is very simple:

COALESCE(value1, value2, ..., n)
sql

You need at least two arguments. If all arguments have the value NULL, NULL is also output at the end.

An example of how it works

You can quickly recognise the basic functionality using a simple example. In the following code, we enter various arguments and check them using the SQL command SELECT.

SELECT COALESCE(NULL, NULL, NULL, 17, 49, NULL, 13, 15, 14, 15);
sql

In this case, the output is ‘17’, as this is the first value that is not ZERO.

The function in combination

The function becomes even more helpful when used in combination with other actions. Let’s imagine a table called ‘Employees’. This table contains the columns ‘Name’, ‘Place of residence’, and ‘Date of birth’. It looks like this:

Name Place of residence Date of birth
Peter Miller Leeds 4/10/1967
Sabine Johnson Leeds 7/27/1989
Sebastian Smith Exeter
Martin Brown Leeds 4/14/2001
Sarah Davis 12/2/2005

Two of the entries are incomplete. Although Sebastian Smith has a place of residence and Sarah Davis has a date of birth, these are not known to us at this time. The SQL COALESCE() function can help make this clear. Here is the appropriate code:

SELECT Name,
COALESCE (Place of residence, 'PLEASE INQUIRE') AS Place of residence,
COALESCE (Date of birth 'PLEASE INQUIRE') AS Date of birth
FROM Employee;
sql

The corresponding output is:

Name Place of residence Date of birth
Peter Miller Leeds 4/10/1967
Sabine Johnson Leeds 7/27/1989
Sebastian Smith Exeter PLEASE INQUIRE
Martin Brown Leeds 4/14/2001
Sarah Davis PLEASE INQUIRE 12/2/2005

Similar functions to SQL COALESCE()

SQL COALESCE() overlaps with a CASE statement and is simply a shortened version of this query option. The CASE code would look like this:

CASE
WHEN value1 IS NOT NULL THEN value1 
WHEN value2 IS NOT NULL THEN value2 
WHEN ... IS NOT NULL THEN ...
ELSE n
END
sql

The ISNULL function is also similar to SQL COALESCE(). Contrary to the function discussed, ISNULL is only evaluated once and doesn’t follow the rules of CASE.

Tip

A server tailored to your needs: With SQL Server Hosting from IONOS, you can use MSSQL, MySQL, or MariaDB and benefit from personal advice, strong security, and outstanding performance.

Was this article helpful?
Page top