TExcelTAD
Home › MySQL › MySQL NULL Functions

MySQL NULL Functions

NULL spreads through expressions: price * NULL is NULL, CONCAT('a', NULL) is NULL. MySQL has several functions for replacing NULLs or producing them on purpose.

Function Returns
IFNULL(a, b) a, or b if a is NULL (MySQL)
COALESCE(a, b, c, ...) the first non-NULL argument (standard SQL)
NULLIF(a, b) NULL if a = b, otherwise a
ISNULL(a) 1 if a is NULL, else 0 (MySQL; not the same as SQL Server's ISNULL)
CONCAT_WS(sep, ...) joins strings with a separator and skips NULLs

IFNULL and COALESCE

mysql · try it yourself
SELECT first_name,
       email,
       IFNULL(email, 'no e-mail') AS with_ifnull,
       COALESCE(email, city, 'no contact') AS best_contact
FROM customers
WHERE customer_id IN (1, 3, 7);
+------------+-----------------------+-----------------------+-----------------------+
| first_name | email                 | with_ifnull           | best_contact          |
+------------+-----------------------+-----------------------+-----------------------+
| Ana        | ana.silva@example.com | ana.silva@example.com | ana.silva@example.com |
| Chloe      | NULL                  | no e-mail             | Paris                 |
| George     | NULL                  | no e-mail             | London                |
+------------+-----------------------+-----------------------+-----------------------+

COALESCE takes any number of arguments, which makes fallback chains easy: e-mail if we have it, otherwise the city, otherwise a fixed text. Prefer it over IFNULL when you want SQL that also runs on other databases.

Fixing calculations

A common use is protecting arithmetic from NULLs. Suppose a promotion table has an optional extra discount:

mysql · try it yourself
CREATE TABLE promo (product_id INT, extra_off DECIMAL(4,2));
INSERT INTO promo VALUES (1, 2.00), (2, NULL), (3, 0.50);

SELECT p.name, p.unit_price, pr.extra_off,
       p.unit_price - pr.extra_off AS naive_price,
       p.unit_price - COALESCE(pr.extra_off, 0) AS safe_price
FROM products AS p
JOIN promo AS pr ON pr.product_id = p.product_id
ORDER BY p.product_id;
+--------------------+------------+-----------+-------------+------------+
| name               | unit_price | extra_off | naive_price | safe_price |
+--------------------+------------+-----------+-------------+------------+
| Espresso Beans 1kg |      24.50 |      2.00 |       22.50 |      22.50 |
| Earl Grey Tea      |       6.75 |      NULL |        NULL |       6.75 |
| Orange Juice 1L    |       3.20 |      0.50 |        2.70 |       2.70 |
+--------------------+------------+-----------+-------------+------------+

The naive calculation lost the price of the tea entirely.

NULLIF: avoiding division by zero

NULLIF(a, b) returns NULL when a equals b. Its classic use is turning a zero divisor into NULL, so that a division returns NULL instead of misleading results:

mysql · try it yourself
SELECT name, units_in_stock,
       ROUND(100 / NULLIF(units_in_stock, 0), 2) AS pct_per_unit
FROM products
WHERE product_id IN (3, 4, 9);
+----------------------+----------------+--------------+
| name                 | units_in_stock | pct_per_unit |
+----------------------+----------------+--------------+
| Orange Juice 1L      |             60 |         1.67 |
| Sparkling Water 6x1L |              0 |         NULL |
| Whole Milk 1L        |            200 |         0.50 |
+----------------------+----------------+--------------+

(MySQL already returns NULL, plus a warning, for a division by zero, and in strict mode an INSERT or UPDATE that divides by zero fails. NULLIF makes the intent explicit and portable.)

Joining text with NULLs

CONCAT returns NULL if any argument is NULL. CONCAT_WS (concatenate with separator) simply skips NULL arguments, which is often what you want for addresses and names:

mysql · try it yourself
SELECT CONCAT(first_name, ', ', city, ', ', country)  AS with_concat,
       CONCAT_WS(', ', first_name, city, country)    AS with_concat_ws
FROM customers
WHERE customer_id IN (8, 9);
+--------------------+--------------------+
| with_concat        | with_concat_ws     |
+--------------------+--------------------+
| Hana, Osaka, Japan | Hana, Osaka, Japan |
| NULL               | Ivan, Bulgaria     |
+--------------------+--------------------+

Test yourself

What does COALESCE(NULL, NULL, 'x', 'y') return?

  1. NULL
  2. 'x'
  3. 'y'
  4. 'xy'

What does NULLIF(5, 5) return?

  1. 5
  2. 0
  3. NULL
  4. 1

What is CONCAT_WS('-', 'a', NULL, 'b')?

  1. NULL
  2. 'a--b'
  3. 'a-b'
  4. 'ab'