mysqldude

NULLIF versus IFNULL

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
IFNULL(NULL,1), IFNULL(3,1) (original)
original post
✓✓✓✓✓✓✓✓✓
COALESCE(NULL,1), COALESCE(NULL,NULL,4) (original)
original post
✓✓✓✓✓✓✓✓✓
NULLIF on the post's five argument pairs (original)
original post
✓✓✓✓✓✓✓✓✓
Common use: x / NULLIF(y, 0) avoids division by zero
modern rewrite
✓✓✓✓✓✓✓✓✓
IFNULL(NULL,1), IFNULL(3,1) (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
COALESCE(NULL,1), COALESCE(NULL,NULL,4) (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
NULLIF on the post's five argument pairs (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Common use: x / NULLIF(y, 0) avoids division by zero modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

The original article

First published 2012-02-28. The text below is the original; the table above shows how it behaves on current versions.

Care has to be taken when using the NULLIF and IFNULL functions.

IFNULL

IFNULL function tests for a NULL and then returns the second parameter. Here is an example:

mysql> SELECT IFNULL(NULL, 1);
+-----------------+
| ifnull(null, 1) |
+-----------------+
|               1 |
+-----------------+
1 row in set (0.00 sec)

mysql> SELECT IFNULL(3, 1);
+--------------+
| ifnull(3, 1) |
+--------------+
|            3 |
+--------------+
1 row in set (0.00 sec)

In the first example we can see that the first parameter is definitely a NULL value, hence, the second parameter is returned. The function COALESCE is very similar to the IFNULL function except the IFNULL limits the number of checks to one parameter, whereas COALESCE can accept multiple parameters returning the first non-NULL as follows:

mysql> select COALESCE(null, 1);
+-------------------+
| COALESCE(null, 1) |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.00 sec)

mysql> select COALESCE(null, null, 4);
+-------------------------+
| COALESCE(null, null, 4) |
+-------------------------+
|                       4 |
+-------------------------+
1 row in set (0.00 sec)
NULLIF

The NULLIF function compares two parameters and returns a NULL if both match, otherwise the first parameter is returned.

We have included some examples below:

mysql> select nullif(null, null);
+--------------------+
| nullif(null, null) |
+--------------------+
| NULL               |
+--------------------+
1 row in set (0.00 sec)

mysql> select nullif(null, 1);
+-----------------+
| nullif(null, 1) |
+-----------------+
| NULL            |
+-----------------+
1 row in set (0.00 sec)


mysql> select nullif(1, null);
+-----------------+
| nullif(1, null) |
+-----------------+
|               1 |
+-----------------+
1 row in set (0.00 sec)

mysql> select nullif(1, 1);
+--------------+
| nullif(1, 1) |
+--------------+
|         NULL |
+--------------+
1 row in set (0.00 sec)

mysql> select nullif(2, 1);
+--------------+
| nullif(2, 1) |
+--------------+
|            2 |
+--------------+
1 row in set (0.00 sec)