SQL NULL Functions
Every major engine ships a small family of helpers for handling NULL values. They differ by name across vendors.
Per-vendor cheat sheet
| DB | Function | Behaviour |
|---|---|---|
| MySQL / MariaDB | IFNULL(x, fallback) | Returns x, or fallback if NULL. |
| SQL Server | ISNULL(x, fallback) | Same as MySQL's IFNULL. |
| MS Access | Nz(x, fallback) | Same idea. |
| All major DBs | COALESCE(x, y, z, …) | Returns the first non-NULL argument. Portable. |
| All major DBs | NULLIF(a, b) | Returns NULL if a = b, else a. Useful for "avoid divide by zero". |
Examples
SQL
SELECT name, COALESCE(phone, 'n/a') AS phone
FROM customers;
-- Avoid divide-by-zero
SELECT total, NULLIF(qty, 0) AS qty,
total / NULLIF(qty, 0) AS unit_price
FROM line_items;
Why COALESCE wins
- Portable — works in every major engine.
- Takes any number of arguments — you can chain fallbacks.
- Short-circuits — stops at the first non-NULL.
Tip: Reach for
COALESCE by default. Save IFNULL/ISNULL for codebases that already use them.Example
Exercise
Portable function that returns the first non-NULL value.
('phone', 'n/a')
Eight letters; the cross-vendor NULL replacement.
Discussion
Loading…