MySQL COALESCE() Function
MySQL COALESCE() Function
The COALESCE() function in MySQL returns the first non-null value from a list of expressions. If all arguments are NULL, it returns NULL.
Syntax
COALESCE(expr1, expr2, …, exprN)
expr1, expr2, …, exprN: These are the expressions to evaluate. The function returns the first non-null expression from this list.
It requires at least two arguments.
It is often used to handle NULL values and provide a default value.
Example
What will be the output of the following query?
mysql> SELECT COALESCE(NULL, NULL, ‘MySQL’, ‘Database’);
A) NULL
B) MySQL
C) Database
D) Error
Correct Answer: B) MySQL
Explanation
The COALESCE() function returns the first non-NULL value from the list of arguments. In this case, the first two arguments are NULL, which returns ‘MySQL’, the first non-NULL value.
MySQL Tutorials
MySQL Tutorials on this website:
For more information on MySQL Database: