limited time offer

What is the purpose of using the IFNULL() function in MySQL?

MySQL IFNULL() takes two expressions and if the first expression is not NULL, it returns the first expression. Otherwise, it returns the second expression.

Depending on the context in which it is used, it returns either numeric or string value.

Example

mysql> SELECT IFNULL(1,2);

+-------------+
| IFNULL(1,2) |
+-------------+
|           1 |
+-------------+

1 row in set (0.00 sec)
mysql> SELECT IFNULL(NULL,2);

+----------------+
| IFNULL(NULL,2) |
+----------------+
|              2 |
+----------------+

1 row in set (0.00 sec)