w3resource

MySQL COALESCE() function

COALESCE() function

MySQL COALESCE() function returns the first non-NULL value of a list, or NULL if there are no non-NULL values.

This function is useful in -

  • It allows you to set a default value when a field is NULL.
  • It returns the first non-NULL value from a list of expressions.
  • This is particularly useful when dealing with potentially null fields, allowing you to provide a default value.
  • This is handy in cases where you want to display a specific value instead of a blank space.
  • It can be used in conjunction with aggregate functions to handle NULL values within calculations or expressions.
  • It helps to prevent errors that may occur due to operations involving NULL values.
  • In dynamic SQL, where values might be unknown or changeable, COALESCE() is useful for providing a fallback value.
  • It aids in data transformation by providing a way to replace NULL values with meaningful alternatives, making it easier to work with the data.

MySQL Version: 8.0

Syntax:

COALESCE(value1,value2,value3,...)

The above syntax is equivalent to the following IF-THEN-ELSE statement

Code:

IF value1 is not NULL THEN
     result = value1;
  ELSIF value2 is not NULL THEN
     result = value2;
  ELSIF value3 is not NULL THEN
     result = value3;
  ELSE
     result = NULL;
  END IF; 
               

Example-1: MySQL COALESCE() function

mysql> SELECT COALESCE(NULL, 2, 3);
+----------------------+
| COALESCE(NULL, 2, 3) |
+----------------------+
|                    2 |
+----------------------+
1 row in set (0.02 sec)
mysql> SELECT COALESCE(NULL, NULL, NULL);
+----------------------------+
| COALESCE(NULL, NULL, NULL) |
+----------------------------+
|                       NULL |
+----------------------------+
1 row in set (0.00 sec)

Pictorial Presentation:

MySQL COALESCE function

Example-2:

The following MySQL statement returns date of establishment for the Jex Max Publication, BPP Publication, Ultra Press Inc., Night Publication and Novel Publisher Ltd. For New Harrold Publication,Mountain Publication Summer, Pieterson Grp. of Publishers, the query returns the country, since they don't have any date of establishment (NULL).

Code:

SELECT pub_NAME,COALESCE(estd,country,pub_city)
FROM newpublisher;

Sample table: newpublisher


Output:

mysql> SELECT pub_NAME,COALESCE(estd,country,pub_city)
-> FROM newpublisher;
+------------------------------+---------------------------------+
| pub_NAME                     | COALESCE(estd,country,pub_city) |
+------------------------------+---------------------------------+
| Jex Max Publication          | 1969-12-25                      | 
| BPP Publication              | 1985-10-01                      | 
| New Harrold Publication      | Australia                       | 
| Ultra Press Inc.             | 1948-07-10                      | 
| Mountain Publication         | USA                             | 
| Summer Night Publication     | 1990-12-10                      | 
| Pieterson Grp. of Publishers | UK                              | 
| Novel Publisher Ltd.         | 2000-01-01                      | 
+------------------------------+---------------------------------+
8 rows in set (0.04 sec)

Difference between IFNULL() and COALESCE() function in MySQL

In MySQL, IFNULL() takes two expressions and if the first expression is not NULL, it returns the first expression otherwise it returns the second expression whereas COALESCE() function returns the first non-NULL value of a list, or NULL if there are no non-NULL values. See the following examples :

mysql> SELECT IFNULL('Red', 'Green');
+------------------------+
| IFNULL('Red', 'Green') |
+------------------------+
| Red                    |
+------------------------+
1 row in set (0.00 sec)
mysql> SELECT IFNULL(NULL, 'Green');
+-----------------------+
| IFNULL(NULL, 'Green') |
+-----------------------+
| Green                 |
+-----------------------+
1 row in set (0.00 sec)
mysql> SELECT COALESCE(NULL, 'Green');
+-------------------------+
| COALESCE(NULL, 'Green') |
+-------------------------+
| Green                   |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT COALESCE(NULL, 'Red', 'Green');
+--------------------------------+
| COALESCE(NULL, 'Red', 'Green') |
+--------------------------------+
| Red                            |
+--------------------------------+
1 row in set (0.00 sec)
mysql> SELECT COALESCE(NULL, NULL, NULL, 'Red');
+-----------------------------------+
| COALESCE(NULL, NULL, NULL, 'Red') |
+-----------------------------------+
| Red                               |
+-----------------------------------+
1 row in set (0.00 sec)

See also : NOT IN(), IS NULL(), LEAST()

Slideshow of MySQL Comparison Function and Operators

Previous: MySQL Comparison functions and operator BETWEEN AND
Next: NULL Safe equal to operator (<=>)



Follow us on Facebook and Twitter for latest update.