NULL Functions: ISNULL, COALESCE, NULLIF
Replace NULLs with ISNULL and COALESCE, create NULLs with NULLIF to avoid divide-by-zero, and know when to use each.
Reports should say "Unsold", not NULL. Display names should fall back to the first name when there is no nickname. And a bowler who faced 0 balls must not crash your strike-rate query. Three NULL functions handle all of this.
Definition
ISNULL(value, replacement_value) returns
valueif it is not NULL; otherwise it returnsreplacement_value. It accepts exactly two arguments (SQL Server only).
COALESCE(value1, value2, value3, ...) returns the first argument that is not NULL, checking from left to right. It accepts two or more arguments and is ANSI-standard. If every argument is NULL, it returns NULL.
NULLIF(value1, value2) returns NULL if the two values are equal; otherwise it returns
value1. It creates a NULL on purpose.
| Function | Job | Arguments | Standard? |
|---|---|---|---|
ISNULL | replace NULL | exactly 2 | SQL Server only |
COALESCE | first non-NULL | 2 or more | ANSI — works everywhere |
NULLIF | create NULL | exactly 2 | ANSI |
Syntax
sql
ISNULL(value, replacement_value)
COALESCE(value1, value2 [, value3 ...])
NULLIF(value1, value2)Example — the functions on literals
sql
SELECT
ISNULL(NULL, 'Unsold') AS isnull_null,
ISNULL('RCB', 'Unsold') AS isnull_value,
ISNULL(NULL, 0) AS isnull_zero,
COALESCE(NULL, NULL, 'Thala') AS coalesce_3,
COALESCE(NULL, 'Dhoni', 'MS') AS coalesce_2,
COALESCE('King', 'Virat') AS coalesce_1,
NULLIF(10, 10) AS nullif_same,
NULLIF(10, 5) AS nullif_diff;| isnull_null | isnull_value | isnull_zero | coalesce_3 | coalesce_2 | coalesce_1 | nullif_same | nullif_diff |
|---|---|---|---|---|---|---|---|
| Unsold | RCB | 0 | Thala | Dhoni | King | NULL | 10 |
Example — ISNULL in a query
sql
SELECT
name,
ISNULL(team, 'Unsold') AS team,
runs
FROM player_profiles;| name | team | runs |
|---|---|---|
| Virat Kohli | RCB | 750 |
| Rohit Sharma | MI | 500 |
| Jos Buttler | RR | 850 |
| MS Dhoni | CSK | 350 |
| David Warner | Unsold | 700 |
| Jasprit Bumrah | MI | 20 |
How it works: step 1 — read the team; step 2 — if it is NULL use 'Unsold', otherwise keep it. Warner has no team, so the report shows 'Unsold'.
Example — COALESCE for a display name
sql
SELECT
first_name,
nickname,
COALESCE(nickname, first_name) AS display_name
FROM player_profiles;| first_name | nickname | display_name |
|---|---|---|
| Virat | King | King |
| Rohit | Hitman | Hitman |
| Jos | NULL | Jos |
| MS | Thala | Thala |
| David | NULL | David |
| Jasprit | NULL | Jasprit |
Show the nickname when it exists, otherwise fall back to the first name. With more arguments you can chain fallbacks: COALESCE(nickname, first_name, name, 'Unknown').
Example — NULLIF to avoid divide-by-zero
A scorecard where Bumrah faced 0 balls:
sql
SELECT
name,
runs,
balls,
ROUND(runs * 100.0 / NULLIF(balls, 0), 1) AS sr
FROM match_scores;| name | runs | balls | sr |
|---|---|---|---|
| Kohli | 45 | 30 | 150 |
| Rohit | 22 | 18 | 122.2 |
| Bumrah | 0 | 0 | NULL |
| Dhoni | 31 | 14 | 221.4 |
How it works: step 1 — NULLIF(balls, 0) turns 0 into NULL; step 2 — anything divided by NULL is NULL. Without NULLIF, SQL Server stops the whole query with "Divide by zero error encountered"; with it you get a harmless NULL. (SQL Server displays the numbers with trailing zeros, e.g. 150.000000. The browser playground returns NULL even without NULLIF — but write NULLIF anyway, because SQL Server won't.)
Example — NULLs in calculations
Any arithmetic with NULL gives NULL, so one missing value can wipe out a total. MS Dhoni has a NULL innings (did not bat):
sql
SELECT
player,
match_date,
runs,
runs + 10 AS plus_bonus,
ISNULL(runs, 0) + 10 AS plus_bonus_safe
FROM innings
WHERE player = 'MS Dhoni';| player | match_date | runs | plus_bonus | plus_bonus_safe |
|---|---|---|---|---|
| MS Dhoni | 2025-03-24 | 26 | 36 | 36 |
| MS Dhoni | 2025-03-31 | NULL | NULL | 10 |
| MS Dhoni | 2025-04-07 | 45 | 55 | 55 |
Warning: Only replace NULL with 0 when "missing" truly means zero. "Did not bat" is not the same as "scored 0" — replacing it changes averages.
ISNULL vs COALESCE
| ISNULL | COALESCE | |
|---|---|---|
| Arguments | exactly 2 | 2 or more |
| Standard | SQL Server only | ANSI (MySQL, PostgreSQL, Oracle …) |
| Result type | type of the first argument (can truncate!) | the highest-precedence type of all arguments |
| Speed | marginally faster | practically the same |
Example of the type trap: if team is VARCHAR(3), ISNULL(team, 'Unsold') can be cut to 'Uns' in SQL Server because the result takes the first argument's type. COALESCE doesn't have this problem. Prefer COALESCE for portable code.
Common mistakes
- ❌
ISNULL(a, b, c)→ error; ISNULL takes exactly 2. ✅COALESCE(a, b, c). - ❌
runs / ballswhen balls can be 0 → divide-by-zero error. ✅runs / NULLIF(balls, 0). - ❌ Mixing types:
ISNULL(runs, 'N/A')— runs is INT, so SQL Server tries to convert 'N/A' to INT and fails. ✅COALESCE(CAST(runs AS VARCHAR(10)), 'N/A'). - ❌ Confusing
ISNULL()(function) withIS NULL(condition).
SQL Server vs MySQL / PostgreSQL
| Task | SQL Server | MySQL | PostgreSQL |
|---|---|---|---|
| Replace NULL (2 args) | ISNULL(a, b) | IFNULL(a, b) | — |
| First non-NULL | COALESCE(...) | COALESCE(...) | COALESCE(...) |
| Create NULL | NULLIF(a, b) | NULLIF(a, b) | NULLIF(a, b) |
Interview tip
Q: How do you prevent a divide-by-zero error in SQL?
Wrap the divisor in NULLIF(divisor, 0). A zero becomes NULL, the division returns NULL instead of failing, and you can then COALESCE the result to 0 or a label if needed.
Key points to remember
- ISNULL(value, replacement) returns the replacement only when value is NULL — exactly 2 arguments, SQL Server only.
- COALESCE(v1, v2, …) returns the first non-NULL argument; ANSI-standard and accepts many arguments.
- NULLIF(a, b) returns NULL when a = b, otherwise a — it creates a NULL on purpose.
- x / NULLIF(divisor, 0) prevents SQL Server's divide-by-zero error.
- Any arithmetic with NULL returns NULL: NULL + 10 = NULL.
- ISNULL's result takes the first argument's type (can truncate); COALESCE is safer and portable.
- MySQL uses IFNULL instead of ISNULL.
Try it yourself
ISNULL and COALESCE on player_profiles
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
NULLIF avoids divide-by-zero
This example runs a small setup step first — click to see it
CREATE TABLE match_scores (name VARCHAR(30), runs INT, balls INT);
INSERT INTO match_scores VALUES ('Kohli', 45, 30), ('Rohit', 22, 18), ('Bumrah', 0, 0), ('Dhoni', 31, 14);Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
NULL in arithmetic and how to guard it
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: a smart label for every player
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
