MySQL COALESCE() Function

In MySQL, the COALESCE() function returns the first value in the parameter list that is not NULL.

COALESCE() Syntax

Here is the syntax of the MySQL COALESCE() function:

COALESCE(value1[, value2 ...])

Parameters

value1[, value2 ...]
Required. The parameter list. You should provide at least one parameter.

Return value

The MySQL COALESCE() function returns the first value in the parameter list that is not NULL. The function will return NULL if all parameters are NULL.

If you do not provide parameters for COALESCE(), MySQL will return an error.

COALESCE() Examples

SELECT COALESCE(NULL, 100), COALESCE(NULL, NULL);
+---------------------+----------------------+
| COALESCE(NULL, 100) | COALESCE(NULL, NULL) |
+---------------------+----------------------+
|                 100 |                 NULL |
+---------------------+----------------------+

here,

  • 100 is the first non-NULL parameter in COALESCE(NULL, 100), so it returned 100.
  • All of the parameters in COALESCE(NULL, NULL) are NULL, so it returned NULL.