Beginner: asking questions · Functions, CASE and NULL · topic 15 of 62 · 30 min
NULL: the missing value
Explanation
What NULL means
NULL is not zero, not an empty text, and not the word "NULL". It is a marker that says "we do not know this value" or "there is no value". Priya Nair has no email in SHOP; Sneha Patil was absent, so her marks are NULL. Zero marks would mean she wrote the exam and scored nothing, which is a very different fact.
Module 2 showed the three-valued logic: a comparison with NULL is neither true nor false but unknown, so = NULL never matches and you test with IS NULL. This topic is about living with NULL in calculations, and replacing it when you need a value.
NULL spreads
| Expression | Result |
|---|---|
| NULL + 5, NULL * 0 | NULL |
| 'Priya' || NULL | NULL |
| UPPER(NULL), LENGTH(NULL) | NULL |
| NULL = NULL, NULL <> 1 | NULL (unknown) |
| '' IS NULL | false: an empty text is a value |
SELECT name || ' <' || email || '>' AS label FROM customers WHERE id IN (2, 3);
-- Ravi Kumar <ravi@example.com>
-- NULL <- Priya's whole label vanishedCOALESCE: the first value that is not NULL
COALESCE(a, b, c, ...) checks its arguments from left to right and returns the first one that is not NULL. With two arguments it means "use a, or b if a is missing". It is standard SQL and works in every major database.
SELECT name, COALESCE(email, 'not given') AS email FROM customers;
-- Asha Verma | asha@example.com
-- Priya Nair | not given
-- Arjun Reddy | not given ... 6 rowsSELECT name, marks, marks + 5 AS with_grace, COALESCE(marks, 0) + 5 AS grace_or_zero
FROM students WHERE id IN (4, 5);
-- Imran Khan | 58 | 63 | 63
-- Sneha Patil | NULL | NULL | 5| Database | Two-argument shortcut | Note |
|---|---|---|
| SQLite, MySQL | IFNULL(x, fallback) | Same as COALESCE(x, fallback) |
| SQL Server | ISNULL(x, fallback) | In MySQL, ISNULL(x) is different: it only tests and returns 1 or 0 |
| Oracle | NVL(x, fallback) | |
| All of them | COALESCE(x, fallback, ...) | Standard; prefer it |
NULLIF: make a value NULL on purpose
NULLIF(a, b) returns NULL when a equals b, otherwise a. Its main use is safe division. A campaign with 0 clicks cannot have a cost per click, and dividing by 0 is an error in PostgreSQL ("division by zero") and SQL Server ("Divide by zero error encountered"). NULLIF(clicks, 0) turns the 0 into NULL, and anything divided by NULL is NULL, so the query runs and that row shows "no value".
SELECT name, ROUND(spend * 1.0 / NULLIF(clicks, 0), 2) AS cost_per_click
FROM campaigns;
-- Diwali Sale | 20.83
-- Monsoon Offer | 26.67
-- New Year Kurta | NULL <- 0 clicks, no crash
-- Holi Colours | 17.78SQLite and MySQL (with default settings) already return NULL for x / 0 instead of failing, so you will not see the error here. Write NULLIF anyway: the query then behaves the same on every database, and it tells the reader you thought about zero.
NULL in sorting, DISTINCT and comparisons
- Sorting: SQLite and MySQL put NULLs first in ascending order, PostgreSQL last. Use NULLS LAST, or sort by COALESCE(marks, -1), to control it (module 2).
- DISTINCT treats all NULLs as one group, so SELECT DISTINCT dept_id shows a single NULL.
- Filters: WHERE dept_id <> 1 skips Deepak, whose dept_id is NULL. To include him, write WHERE dept_id IS DISTINCT FROM 1 (standard SQL, PostgreSQL, SQLite 3.39+, SQL Server 2022) or SQLite's shorter dept_id IS NOT 1.
- Counting: COUNT(*) counts rows, COUNT(marks) counts only rows where marks is not NULL. Module 4 builds on this.
Common mistakes
- Testing with = NULL. Use IS NULL / IS NOT NULL.
- Replacing NULL with 0 everywhere. An unknown amount is not a zero amount; averages and totals change. Decide per report.
- Joining text pieces with || when one can be NULL, and losing the whole value. Wrap each optional piece in COALESCE.
- Dividing by a column that can be 0. Use NULLIF(column, 0).
- Using ISNULL(x, 0) in MySQL. There ISNULL takes one argument; use IFNULL or COALESCE.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
NULL: the missing value · 1/7
NULL means missing or unknown. It is not zero, and it is not an empty text.
Prefer a video by a teacher? Find free videos on “sql null coalesce nullif ifnull isnull tutorial” on YouTube ↗
In real life: Ad reports in a marketing dashboard
Marketing dashboards show cost per click and conversion rate for each campaign. A brand-new campaign may have zero clicks, and an old one may have no order data. Analysts use NULLIF so a zero never crashes the report and COALESCE so blanks show a clear label, instead of letting one odd row break the whole dashboard.
SELECT name,
ROUND(spend * 1.0 / NULLIF(clicks, 0), 2) AS cost_per_click,
ROUND(orders * 100.0 / NULLIF(clicks, 0), 1) AS conversion_pct,
COALESCE(orders, 0) AS orders_shown
FROM campaigns;Practice (0/4 done)
From customers, return name and email, but show 'not given' when the email is missing. Name the second column email.
From campaigns, return name and the cost per click (spend divided by clicks) rounded to 2 decimal places, named cost_per_click. Use NULLIF so a campaign with 0 clicks gives NULL instead of an error on other databases, and avoid integer division.
How many students have no marks recorded? Return one column named missing_marks.
List everyone who is NOT in department 1, including people with no department. Return name and dept_id from employees (COMPANY).
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Working with datesFunctions, CASE and NULL · same module
- Numbers and mathsFunctions, CASE and NULL · same module
- SELECT and WHERE: finding rowsBackend Development · Databases and SQL basics
- SELECT, columns, aliases and expressionsSELECT: filtering and sorting
- MySQL vs PostgreSQL vs SQLite vs SQL ServerProcedures, triggers, SQL engines and NoSQL
- DISTINCT and how a query runsSELECT: filtering and sorting
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does COALESCE(NULL, NULL, 'Pune', 'Delhi') return?
Answer: 'Pune'
COALESCE returns the first argument that is not NULL.
What does NULLIF(0, 0) return?
Answer: NULL
NULLIF gives NULL when the two values are equal.
Which is the SQL Server function for "use x, or 0 when x is NULL"?
Answer: ISNULL(x, 0)
SQL Server has ISNULL(x, 0); COALESCE(x, 0) also works there and everywhere else.
Why write spend / NULLIF(clicks, 0) instead of spend / clicks?
Answer: Dividing by 0 is an error in PostgreSQL and SQL Server; NULLIF makes it NULL instead
Anything divided by NULL is NULL, so the query keeps running.
Sneha has NULL marks. Which statement is true?
Answer: marks + 5 for her is NULL
Arithmetic with NULL gives NULL; use IS NULL to find her, and NULL sorting differs between databases.
Why does WHERE email = NULL return no rows?Related
Answer: Comparing with NULL gives unknown, and WHERE keeps only true rows
Use IS NULL to test for missing values.
How do you find rows where email is missing?Related
Answer: WHERE email IS NULL
NULL is not equal to anything, so = NULL never matches. Use IS NULL.
Deepak has dept_id NULL. Does WHERE dept_id <> 1 return him?Related
Answer: No, because the comparison gives unknown, and WHERE keeps only true
Any comparison with NULL gives unknown; use IS NULL to find such rows.
What is marks + 5 when marks is NULL?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- This is the last topic of “Functions, CASE and NULL”. The module exam comes next.
- Module 3 exam: Functions, CASE and NULL · 15 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Aggregation: GROUP BY and HAVING. 62 topics left in the course before the final exam.