A Beginner’s Guide to the COALESCE Function in SQL

Syed Ali
8 min readDec 22, 2022

--

Coalesce Art by DALL-E 2
Coalesce Art — DALL-E 2

The word “coalesce” means to merge or come together, and it is derived from the Latin word “coalescere,” which means “to grow together.” In the context of the COALESCE function in SQL, it refers to the way in which the function combines or “merges” multiple expressions into a single result. Hence the Coalesce Art.

The COALESCE function in SQL is used to return the first non-null expression among its arguments. The syntax of the COALESCE function is as follows:

COALESCE(expression1, expression2, ... expression_n)

Here, expression1, expression2, … expression_n are the expressions that you want to evaluate. If expression1 is not null, the COALESCE function will return it. If expression1 is null, the COALESCE function will evaluate expression2 and return it if it is not null. If expression2 is also null, the COALESCE function will evaluate the next expression and so on, until it finds a non-null expression. If all expressions are null, the COALESCE function will return null.

Here is an example of using the COALESCE function in a SELECT statement:

SELECT COALESCE(col1, col2, col3) FROM table_name;

This will return the value of col1 if it is not null, or the value of col2 if col1 is null and col2 is not null, or the value of col3 if both col1 and col2 are null and col3 is not null. If all three columns are null, the COALESCE function will return null.

Note that the COALESCE function is not the same as the NULLIF function, which returns null if its two arguments are equal, or the first argument if they are not equal.

Brilliant, right? No..!?

I wonder, what if we can see the above mumbo jumbo in action with some real data….

Sure, why not, but remember you asked for it.

Suppose you have a table called employees with the following structure and data:

To get the name and salary of all employees, along with the name of their manager if they have one, you can use the following SELECT statement:

SELECT e.id, e.name, e.salary, COALESCE(m.name, 'None') AS manager
FROM employees e
LEFT JOIN employees m ON e.manager = m.id;

This will return the following results:

Note that the COALESCE function is used to get the name of the manager if it is not null, or ‘None’ if the manager is null. The LEFT JOIN is used to include all employees in the results, even if they don’t have a manager.

Interesting. But what if there is a way to understand the SQL syntax from a newbie’s perspective?

Sure, let’s dissect the SQL Query.

Here is a breakdown of the SQL query in simple terms:

SELECT e.id, e.name, e.salary, COALESCE(m.name, 'None') AS manager
FROM employees e
LEFT JOIN employees m ON e.manager = m.id;
  • SELECT: This is the keyword that indicates the start of a SELECT statement. It is followed by a list of the columns that you want to include in the results.
  • e.id, e.name, e.salary: These are the columns from the employees table that you want to include in the results. The e before each column name is an alias for the employees table, which is used to avoid ambiguity when joining multiple tables.
  • COALESCE(m.name,’None') AS manager: This is the COALESCE function, which returns the value of m.name if it is not null, or ‘None’ if it is null. The m before name is an alias for the employees table, which is used to identify the column as coming from the employees table. The AS managerpart gives the resulting column a name of ‘manager’ in the results.
  • FROM employee e: This specifies the name of the table that you want to select data from, which is the employees table in this case. The e after the table name is an alias for the employees table, which is used to avoid ambiguity when joining multiple tables.
  • LEFT JOIN employees m ON e.manager = m.id: This specifies a LEFT JOIN clause, which includes all rows from the employees table (the table on the left side of the JOIN clause) in the results, even if there is no matching row in the employees table on the right side of the JOIN clause. The m after the employees table name is an alias for the employees table on the right side of the JOIN clause, which is used to identify the columns as coming from that table. The ON e.manager = m.id part specifies the condition that determines which rows are matched in the two tables. In this case, the rows are matched if the manager column in the employees table on the left side of the JOIN clause (e.manager) is equal to the id column in the employees table on the right side of the JOIN clause (m.id). This allows you to get the name of the manager for each employee.

Whew!! My head hurts.

However, if it is still hard to grasp the flow of the query; let’s visualize its step-by-step execution on the given dataset.

  • ✦1✦ The FROM clause specifies the employees table as the source of the data. This table has four rows:
  • ✦2✦ The LEFT JOINclause specifies a LEFT JOIN with the employeestable on the right side. This means that all rows from the employees table on the left side (the table specified in the FROM clause) will be included in the results, even if there is no matching row in the employees table on the right side.
  • ✦3✦ The ON clause specifies the condition that determines which rows are matched in the two tables. In this case, the rows are matched if the manager column in the employees table on the left side is equal to the id column in the employees table on the right side. This allows you to get the name of the manager for each employee.
  • ✦4✦ The SELECT clause specifies the columns that you want to include in the results. The COALESCE function is used to get the name of the manager if it is not null, or ‘None’ if it is null.
  • ✦5✦ The final results of the query are as follows:

YOU NAILED IT!!

But… But WHAT?

Isn’t SQL supposed to comprise English-like words? Where did this COALESCE come from?

Arrgh… This is an SQL lecture, not English literature… Anyways, I knew you would ask something like this, you nerd! Go back to the top and read the first paragraph again!

COOL!!

By the way, aren’t there any other easier ways to achieve the same functionality as COALESCE function?

Ah, I thought you would never ask.

There are a few other ways to achieve similar functionality as the COALESCE function in SQL, depending on your specific needs and the capabilities of your database management system (DBMS). Here are a few options you might consider:

  • ✦1✦ The ISNULLfunction: Many DBMSs, including Microsoft SQL Server and MySQL, provide an ISNULL function that works in a similar way to the COALESCE function. The syntax for ISNULL is as follows:
ISNULL(expression, replacement_value)

This function returns the value of expression if it is not null, or replacement_value if expression is null. For example:

SELECT ISNULL(col1, 0) FROM table_name;

This will return the value of col1 if it is not null, or 0 if it is null.

  • ✦2✦ The NVL function: The NVL function is similar to the COALESCE and ISNULLfunctions, and it is available in some DBMSs, including Oracle and MySQL. The syntax for NVL is as follows:
NVL(expression, replacement_value)

This function returns the value of expression if it is not null, or replacement_value if expression is null. For example:

SELECT NVL(col1, 0) FROM table_name;

This will return the value of col1 if it is not null, or 0 if it is null.

  • ✦3✦ The CASE statement: You can also use a CASE statement to achieve similar functionality as the COALESCE function. The syntax for a CASE statement is as follows:
CASE
WHEN expression1 THEN result1
WHEN expression2 THEN result2
...
ELSE result_else
END

This statement evaluates each expression in turn, and returns the corresponding result if the expression is true. If none of the expressions are true, the ELSE clause is used to specify the result to be returned. For example:

SELECT
col1,
CASE
WHEN col1 IS NOT NULL THEN col1
ELSE 0
END AS col1_or_zero
FROM table_name;

This will return the value of col1 if it is not null, or 0 if it is null.

BRILLIANT…

How popular is COALESCE function in terms of usage among the most used functions or keywords in SQL?

It’s difficult to say exactly how frequently the COALESCE function is used compared to other functions and keywords in SQL, as it can vary depending on the specific needs of the database and the queries being written. However, the COALESCE function is certainly a useful and commonly used function in SQL.

The COALESCE function is often used to handle null values in SQL queries. It allows you to specify a default value to use if a particular expression is null, which can be helpful for avoiding null values in the results of your queries. For example, you might use the COALESCE function to substitute a default value for a null value in a column, or to choose a non-null value from a list of possible expressions.

Overall, the COALESCE function is a useful tool in the SQL developer’s toolkit, and it is likely to be used frequently in many SQL queries. However, it is just one of many functions and keywords available in SQL, and its relative importance will depend on the specific needs and goals of your queries.

TOO MUCH TEXT…

Well, is there anything else to learn about COALESCE function? I need to use the powder room.

Of course, here are a few additional things you might want to know about the COALESCE function in SQL:

  • ✦1✦ The COALESCE function can accept any number of expressions as arguments. For example, you can use the COALESCE function to choose the first non-null value from a list of multiple expressions, like this:
COALESCE(expression1, expression2, expression3, ... expression_n)
  • ✦2✦ The COALESCE function is often used in combination with other functions and operators in SQL. For example, you can use the COALESCE function with the SUM function to sum a list of values, replacing any null values with a default value:
SELECT SUM(COALESCE(col1, 0)) FROM table_name;

This will return the sum of col1, replacing any null values with 0.

  • The COALESCE function can be used in the SELECT, INSERT, UPDATE, and DELETE statements, as well as in the WHERE, GROUP BY, and HAVING clauses.
  • Some DBMSs support an alternative syntax for the COALESCE function, using the OR operator instead of the COALESCE keyword. For example, in MySQL, you can use the following syntax:
SELECT expression1 OR expression2 OR ... OR expression_n;

This syntax is equivalent to the COALESCE function, and it returns the first non-null expression among its arguments.

Congratulations!

You have finally reached the end of the internet!

* Please don’t forget to turn off the lights on your way out.

--

--

Syed Ali
Syed Ali

Written by Syed Ali

With over a decade of experience, Syed Ali is an IT expert with a passion for data analytics and research & development.

No responses yet