Mysql Coalesce Function


This tutorial explains how to use the MySQL COALESCE function with syntax and examples.


The MySQL COALESCE function returns the first non-null expression in the list.


The syntax for the COALESCE function in MySQL is:

COALESCE( expression1, expression2, ... expression_n )

Parameters or Arguments

expression1 to expression_n

The expressions to test for non-null values.


  • If all expressions evaluate to null, then the COALESCE function will return null.

Applies To

The COALESCE function can be used in the following versions of MySQL:

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23


Let's look at some MySQL COALESCE function examples and explore how to use the COALESCE function in MySQL.

For example:

mysql> SELECT COALESCE(null, null, null, 'A', 'B');
Output: 'A'

mysql> SELECT COALESCE('A', 'B', null, 'C', 'D');
Output: 'A'

mysql> SELECT COALESCE(null, 1, 2, 3, null, 4);
Output: 1

mysql> SELECT COALESCE(null, '', '');
Output: ''

mysql> SELECT COALESCE(null, null, null, null, null);
Output: NULL