← Back to SQL, page 2

sql-0034 · Easy fresher

Explain COALESCE, IFNULL and NULLIF with an example of each.

COALESCE(a, b, c) returns the first argument that is not NULL, and it is standard SQL. IFNULL(a, b) is a two-argument version found in SQLite and MySQL; SQL Server calls it ISNULL. NULLIF(a, b) does the reverse job: it returns NULL when a equals b, and a otherwise, which is handy for turning placeholder values such as 0 or '' into NULL.

Full answer

These three functions convert between NULL and real values.

COALESCE walks its arguments left to right and returns the first non-NULL one. A contact table where people may give a mobile, a home phone or neither:

sql
CREATE TABLE contacts (name TEXT, mobile TEXT, home TEXT);
INSERT INTO contacts VALUES ('Ana','555-0101',NULL),('Ben',NULL,'555-0199'),('Chen',NULL,''),('Dara',NULL,NULL);
sql
SELECT name, COALESCE(mobile, home, 'no phone') AS best_phone FROM contacts ORDER BY name;
namebest_phone
Ana555-0101
Ben555-0199
Chen''
Darano phone

IFNULL(a, b) is a two-argument shortcut in SQLite and MySQL, the same as COALESCE(a, b). SQL Server's version is ISNULL(a, b), and Oracle's is NVL(a, b). Prefer COALESCE in portable code.

sql
SELECT name, IFNULL(mobile, 'no mobile') AS mobile FROM contacts ORDER BY name;
namemobile
Ana555-0101
Benno mobile
Chenno mobile
Darano mobile

NULLIF(a, b) goes the other way. If a = b it returns NULL, otherwise it returns a. Its classic use is cleaning values that mean "nothing" but were stored as something:

sql
SELECT name,
       COALESCE(mobile, home, 'no phone')               AS without_nullif,
       COALESCE(mobile, NULLIF(home, ''), 'no phone')   AS with_nullif
FROM contacts
ORDER BY name;
namewithout_nullifwith_nullif
Ana555-0101555-0101
Ben555-0199555-0199
Chen''no phone
Darano phoneno phone

Chen's empty home phone became NULL, so COALESCE could skip it. Without NULLIF, COALESCE('', 'no phone') returns the empty string, since an empty string is a value, not a NULL.

Another everyday use of NULLIF is guarding a division: total / NULLIF(count, 0) gives NULL instead of a division error when count is zero.

sql
CREATE TABLE contacts (name TEXT, mobile TEXT, home TEXT);
INSERT INTO contacts VALUES ('Ana','555-0101',NULL),('Ben',NULL,'555-0199'),('Chen',NULL,''),('Dara',NULL,NULL);

SELECT name, COALESCE(mobile, home, 'no phone') AS best_phone FROM contacts ORDER BY name;

SELECT name, IFNULL(mobile, 'no mobile') AS mobile FROM contacts ORDER BY name;

SELECT name,
       COALESCE(mobile, home, 'no phone')               AS without_nullif,
       COALESCE(mobile, NULLIF(home, ''), 'no phone')   AS with_nullif
FROM contacts
ORDER BY name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • COALESCE returns the first non-NULL argument and takes any number of arguments
  • IFNULL, ISNULL and NVL are two-argument vendor versions
  • NULLIF(a, b) returns NULL when a = b
  • NULLIF(x, '') then COALESCE handles empty strings

Common mistakes

  • Expecting COALESCE to skip empty strings
  • Using vendor-specific ISNULL or NVL in code meant to be portable

Follow-ups

  • How would you protect a percentage calculation from division by zero?
  • Does COALESCE evaluate every argument?

Tips

  • Pair NULLIF with COALESCE in your example to show both directions