SQL NULL Functions
NULL values spread through expressions — price * NULL is NULL, 'a' || NULL is NULL. A few functions help you replace or create NULLs on purpose.
| Function | Returns | Available in |
|---|---|---|
COALESCE(a, b, …) |
the first non-NULL argument | all databases (standard) |
IFNULL(a, b) |
a if not NULL, else b |
SQLite, MySQL |
ISNULL(a, b) |
same | SQL Server |
NVL(a, b) |
same | Oracle |
NULLIF(a, b) |
NULL if a = b, else a |
all databases (standard) |
COALESCE: a fallback value
Show "-" for customers without e-mail:
SELECT name, COALESCE(email, '-') AS email
FROM customers
WHERE customer_id IN (4, 5, 12)
ORDER BY customer_id;name | email
----------------+---------------------
Pixel Studio | noah.kim@example.com
Le Petit Marche | -
Omar Murphy | -
(3 rows)
COALESCE accepts any number of arguments and returns the first that is not NULL. Use the shipping date if there is one, otherwise the order date:
SELECT order_id, status,
COALESCE(shipped_date, order_date) AS last_activity
FROM orders
WHERE order_id BETWEEN 7 AND 11
ORDER BY order_id;order_id | status | last_activity
---------+------------+--------------
7 | delivered | 2025-02-21
8 | delivered | 2025-02-19
9 | processing | 2025-03-24
10 | shipped | 2025-03-14
11 | cancelled | 2025-03-18
(5 rows)
IFNULL
IFNULL(a, b) is the two-argument version. It is handy in calculations — here, after a LEFT JOIN, products without sales would show NULL units:
SELECT p.name, IFNULL(SUM(oi.quantity), 0) AS units_sold
FROM products AS p
LEFT JOIN order_items AS oi ON oi.product_id = p.product_id
WHERE p.category_id = 7
GROUP BY p.product_id
ORDER BY p.name;name | units_sold
---------------+-----------
Cast Iron Pan | 19
Chef Knife | 9
Coffee Grinder | 0
(3 rows)
NULLIF: turning a value into NULL
NULLIF(a, b) returns NULL when the two values are equal. The classic use is avoiding division by zero: dividing by NULL yields NULL instead of an error (or, in SQLite, instead of a silent NULL from x / 0):
SELECT name, price, stock,
ROUND(price / NULLIF(stock, 0), 3) AS price_per_unit_in_stock
FROM products
WHERE product_id IN (3, 4, 13, 14)
ORDER BY product_id;name | price | stock | price_per_unit_in_stock
----------------+-------+-------+------------------------
Orange Juice | 3.2 | 75 | 0.043
Sparkling Water | 4.6 | 0 | NULL
Apples | 2.99 | 150 | 0.02
Avocados | 4.5 | 0 | NULL
(4 rows)
Another use is treating "empty" placeholder values as missing:
CREATE TABLE contacts (name TEXT, phone TEXT);
INSERT INTO contacts VALUES ('Ava', '555-0101'), ('Liam', ''), ('Sofia', NULL);
SELECT name,
COALESCE(NULLIF(phone, ''), 'no phone') AS phone
FROM contacts
ORDER BY name;3 rows affected.
name | phone
------+---------
Ava | 555-0101
Liam | no phone
Sofia | no phone
(3 rows)
NULL in arithmetic
Imagine a discount column that is NULL for most products. price - discount would be NULL for them; wrap the nullable part:
ALTER TABLE products ADD COLUMN discount REAL;
UPDATE products SET discount = 2 WHERE product_id IN (22, 23);
SELECT name, price, discount,
price - discount AS naive,
price - COALESCE(discount, 0) AS final_price
FROM products
WHERE product_id IN (21, 22, 23)
ORDER BY product_id;2 rows affected.
name | price | discount | naive | final_price
----------------------------+-------+----------+-------+------------
Wireless Mouse | 24.99 | NULL | NULL | 24.99
Mechanical Keyboard | 89 | 2 | 87 | 87
Noise-Cancelling Headphones | 149 | 2 | 147 | 147
(3 rows)
Tip: Prefer
COALESCEin new code: it is standard SQL, works everywhere, and takes more than two arguments.
Test yourself
What is COALESCE(NULL, NULL, 'x', 'y')?
- NULL
- 'x'
- 'y'
- 'xy'
What does NULLIF(stock, 0) return when stock is 0?
- 0
- NULL
- 1
- An error
Which function is the SQL Server equivalent of SQLite's IFNULL?
- NVL
- ISNULL
- NULLIF
- IFNULL