Handling NULL Values in SQL: IS NULL, COALESCE & Beyond
NULL represents an unknown or missing value in SQL. It is not the same as zero or an empty
string, and it behaves differently in comparisons, joins, and aggregates. Handling NULL
correctly is one of the most important SQL skills. This guide covers IS NULL, COALESCE, NULLIF,
NVL, IFNULL, NULL in joins and aggregates, and the 25 interview questions that actually come up
— with deep, descriptive theory aligned to the topic.
🎯 Key Takeaways
NULL means unknown or missing, not zero or empty
IS NULL is the correct way to check for NULL
COALESCE returns the first non-NULL value
NULLIF converts values to NULL
NULL in joins does not match
NULL in aggregates is ignored
25 Interview QuestionsBeginner to AdvancedOracle, MySQL, PostgreSQL, SQL ServerUpdated Dec 3, 2026
KW
Kailas Warade
✓ Verified Author
Data Engineer · Oracle Certified · SQL & PL/SQL
Kailas works with SQL, PL/SQL, Python, and PySpark to build data pipelines.
He writes the SQL interview guides at Sankalan Data Tech, focused on
practical, real-world answers.
🏆 Oracle Certified💼 Data Engineer📝 SQL & PL/SQL Guides📍 Pune, India
NULL represents an unknown or missing value in SQL. It is not the same as zero or an empty
string.
-- NULL is not zero
NULL ≠ 0
-- NULL is not empty string
NULL ≠ ''
-- NULL means unknown
NULL = unknown
The key insight is that NULL means "we don't know". It is not a value — it is the absence
of a value.
This has profound implications for how NULL behaves in comparisons, joins, and aggregates.
Understanding NULL is essential for writing correct SQL.
NULL is one of the most misunderstood concepts in SQL, and questions about it come up in
almost every interview.
Why NULL Handling Matters
NULL handling matters because NULL behaves differently from regular values in almost every
SQL operation.
Comparisons. Any comparison with NULL returns unknown, not true or false.
This is why = NULL does not work.
Joins. NULL values do not match in JOINs. A NULL in one table will not
join with a NULL in another table.
Aggregates. Aggregate functions like SUM, AVG, COUNT ignore NULL values.
This affects calculations.
Sorting. NULL sorting behavior varies by database, affecting query
results.
Handling NULL correctly is essential for data quality and correct query results.
IS NULL in Depth
IS NULL is the correct way to check for NULL in SQL. You cannot use = NULL because NULL
comparisons return unknown.
-- Correct: IS NULL
SELECT * FROM employees WHERE salary IS NULL;
-- Incorrect: = NULL (always returns unknown)
SELECT * FROM employees WHERE salary = NULL;
IS NULL checks if a value is NULL. IS NOT NULL checks if a value is not NULL.
This is one of the most common SQL interview questions. Knowing the difference between IS
NULL and = NULL is essential.
IS NULL works across all databases — it is ANSI standard.
COALESCE in Depth
COALESCE returns the first non-NULL value from a list of expressions. It is ANSI standard
and works across databases.
-- COALESCE: first non-NULL value
SELECT COALESCE(NULL, NULL, 'default') AS result;
-- Returns: 'default'
SELECT COALESCE(phone, mobile, 'No phone') AS contact
FROM employees;
COALESCE evaluates arguments from left to right and returns the first non-NULL value. If
all are NULL, it returns NULL.
COALESCE is the preferred function for handling NULL because it is standard and flexible.
COALESCE can take multiple arguments, unlike NVL and IFNULL which take only two.
NULLIF in Depth
NULLIF returns NULL if two expressions are equal, otherwise returns the first expression.
-- NULLIF: returns NULL if equal
SELECT NULLIF('A', 'A') AS result;
-- Returns: NULL
SELECT NULLIF('A', 'B') AS result;
-- Returns: 'A'
NULLIF is used to convert values to NULL, often to avoid division by zero.
-- Avoid division by zero
SELECT amount / NULLIF(quantity, 0) AS unit_price
FROM orders;
NULLIF is the inverse of COALESCE — it creates NULL rather than handling it.
NVL and IFNULL
NVL is an Oracle-specific function that returns the second value if the first is NULL.
IFNULL is a MySQL-specific function that does the same.
-- Oracle: NVL
SELECT NVL(phone, 'No phone') FROM employees;
-- MySQL: IFNULL
SELECT IFNULL(phone, 'No phone') FROM employees;
-- Standard: COALESCE
SELECT COALESCE(phone, 'No phone') FROM employees;
NVL and IFNULL are limited to two arguments. COALESCE is standard and flexible.
COALESCE is preferred because it works across all databases.
Knowing the database-specific functions is important for interviews.
NULL in Joins
NULL values do not match in JOINs. A NULL in one table will not join with a NULL in
another table using standard equality.
-- NULL does not match in JOIN
SELECT *
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id;
-- NULL department_id will not match
This is because NULL = NULL returns unknown, not true. So the JOIN condition fails.
To match NULLs, use IS NOT DISTINCT FROM (PostgreSQL) or handle it explicitly.
Understanding NULL in joins is essential for correct query results.
NULL in Aggregates
Aggregate functions like SUM, AVG, COUNT ignore NULL values.
-- SUM ignores NULL
SELECT SUM(salary) FROM employees; -- NULL salaries ignored
-- COUNT(*) counts all rows
SELECT COUNT(*) FROM employees;
-- COUNT(column) counts non-NULL
SELECT COUNT(salary) FROM employees; -- NULL salaries not counted
COUNT(*) counts all rows. COUNT(column) counts only non-NULL values. This is a common
interview question.
AVG ignores NULL, which affects the result. If you want to include NULL as zero, use
COALESCE.
25 NULL Handling Interview Questions and Answers
These are the NULL handling questions that come up most often in SQL interviews. Every
answer stands on its own — read them in any order, or work through them in sequence to
build a complete understanding of NULL in SQL, IS NULL, COALESCE, NULLIF, NVL, IFNULL,
NULL in joins and aggregates.
/
🔍
No matching questions found
Try a keyword like IS NULL, COALESCE, NULLIF, or NVL.
NULL represents an unknown or missing value in SQL. It is not the same as zero or an
empty string.
NULL ≠ 0
NULL ≠ ''
NULL = unknown
The key insight is that NULL means "we don't know". It is not a value — it is the
absence of a value.
This has profound implications for how NULL behaves in SQL operations.
You use IS NULL or IS NOT NULL. You cannot use = NULL because NULL comparisons
return unknown.
-- Correct
SELECT * FROM employees WHERE salary IS NULL;
-- Incorrect
SELECT * FROM employees WHERE salary = NULL;
IS NULL checks if a value is NULL. IS NOT NULL checks if a value is not NULL.
This is one of the most common SQL interview questions.
COALESCE returns the first non-NULL value from a list of expressions.
SELECT COALESCE(NULL, NULL, 'default') AS result;
-- Returns: 'default'
COALESCE evaluates arguments from left to right and returns the first non-NULL
value.
COALESCE is ANSI standard and works across all databases.
NULLIF returns NULL if two expressions are equal, otherwise returns the first
expression.
SELECT NULLIF('A', 'A') AS result;
-- Returns: NULL
NULLIF is used to convert values to NULL, often to avoid division by zero.
NULLIF is the inverse of COALESCE.
NVL is an Oracle-specific function that returns the second value if the first is
NULL.
SELECT NVL(phone, 'No phone') FROM employees;
NVL is limited to two arguments. COALESCE is standard and flexible.
NVL is Oracle-specific.
IFNULL is a MySQL-specific function that returns the second value if the first is
NULL.
SELECT IFNULL(phone, 'No phone') FROM employees;
IFNULL is limited to two arguments. COALESCE is standard and flexible.
IFNULL is MySQL-specific.
Any comparison with NULL returns unknown, not true or false.
NULL sorting behavior varies by database. Use NULLS FIRST/LAST to control it.
-- PostgreSQL: NULLs last in ASC
ORDER BY salary ASC NULLS LAST
-- Oracle: NULLs last in ASC
ORDER BY salary ASC NULLS LAST
NULL sorting is database-specific. Always specify NULLS FIRST/LAST for predictable
results.
IS NULL checks for NULL correctly. = NULL always returns unknown.
-- Correct
WHERE salary IS NULL
-- Incorrect
WHERE salary = NULL
This is the most common NULL interview question.
Use IS NULL or IS NOT NULL in WHERE clauses. Never use = NULL.
SELECT * FROM employees
WHERE salary IS NULL;
SELECT * FROM employees
WHERE salary IS NOT NULL;
This is the correct way to filter NULL values.
Use IS NULL in CASE expressions to handle NULL values.
SELECT
CASE
WHEN salary IS NULL THEN 'Unknown'
ELSE 'Known'
END AS salary_status
FROM employees;
CASE expressions need explicit NULL handling because NULL comparisons return
unknown.
NULL in string concatenation makes the entire result NULL in most databases.
-- NULL propagates
SELECT 'Hello ' || NULL FROM dual;
-- Returns: NULL
-- Handle with COALESCE
SELECT 'Hello ' || COALESCE(name, '') FROM employees;
Use COALESCE to provide a default for NULL in concatenation.
NULL in arithmetic makes the entire result NULL.
-- NULL propagates
SELECT 5 + NULL FROM dual;
-- Returns: NULL
-- Handle with COALESCE
SELECT 5 + COALESCE(discount, 0) FROM orders;
Use COALESCE to provide a default for NULL in arithmetic.
NULL means unknown/missing. Zero is a known value.
NULL ≠ 0
-- NULL: unknown salary
-- 0: salary is zero
This distinction matters for business logic. A NULL salary is unknown; a zero salary
is known to be zero.
NULL values form their own group in GROUP BY.
SELECT department_id, COUNT(*)
FROM employees
GROUP BY department_id;
-- NULL department_id forms its own group
NULL is treated as a distinct value in GROUP BY, forming its own group.
NULL is treated as a single value in DISTINCT.
SELECT DISTINCT department_id FROM employees;
-- NULL appears once, even if there are multiple NULLs
DISTINCT treats all NULLs as one value.
NULL in subqueries affects IN and NOT IN behavior.
-- NOT IN with NULL returns no rows
SELECT * FROM employees
WHERE department_id NOT IN (SELECT department_id FROM departments);
-- If subquery returns NULL, NOT IN returns no rows
NULL in NOT IN subqueries is a common gotcha. Use NOT EXISTS instead.
NULL handling in indexes varies by database. Oracle does not index NULL by default;
PostgreSQL does.
-- Oracle: NULL not indexed by default
-- PostgreSQL: NULL indexed
-- Oracle workaround: function-based index
CREATE INDEX idx ON employees(COALESCE(salary, 0));
NULL indexing affects query performance. Be aware of your database's behavior.
Well-designed NULL handling follows a few key principles.
Use IS NULL for filtering — never = NULL.
Use COALESCE for defaults — standard and flexible.
Be aware of NULL in joins — NULL does not match.
Be aware of NULL in aggregates — ignored by SUM, AVG, COUNT.
Use NOT EXISTS instead of NOT IN — to avoid NULL gotchas.
Use NULLS FIRST/LAST — for predictable sorting.
-- Well-designed NULL handling
SELECT COALESCE(phone, 'No phone') AS contact
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary ASC NULLS LAST;
These principles produce SQL that handles NULL correctly and predictably.
Mistakes I Keep Seeing in Interviews
These come up repeatedly. None of them are exotic, and every one of them is easy to avoid
once you know it is coming.
Mistake one: using = NULL instead of IS NULL. This is the most common NULL
mistake and a red flag in interviews.
Mistake two: not understanding NULL in joins. NULL does not match, which
affects JOIN results.
Mistake three: not understanding NULL in aggregates. SUM, AVG, COUNT ignore
NULL, affecting calculations.
Mistake four: confusing NULL with zero or empty string. They are different
concepts.
Mistake five: using NOT IN with NULL subqueries. This returns no rows due
to NULL behavior.
Mistake six: not using COALESCE for defaults. This leads to NULL
propagation in calculations and concatenations.
Follow-Up Questions Interviewers Ask
Getting the first answer right is only half the interview. What usually decides the
outcome is what happens next. Here is what to prepare for on the NULL handling topic.
After “What is NULL?” — expect
“How do you check for NULL?”
“What is the difference between NULL and zero?”
“What is the difference between NULL and empty string?”
“How does NULL behave in comparisons?”
After “COALESCE” — expect
“What is the difference between COALESCE and NVL?”
“What is the difference between COALESCE and NULLIF?”
“How many arguments does COALESCE take?”
“When would you use COALESCE?”
After “NULL in joins” — expect
“Does NULL match in JOINs?”
“How do you handle NULL in JOINs?”
“What is IS NOT DISTINCT FROM?”
“How does NULL affect LEFT JOIN?”
After “NULL in aggregates” — expect
“What is the difference between COUNT(*) and COUNT(column)?”
“How does AVG handle NULL?”
“How does SUM handle NULL?”
“How do you include NULL as zero in aggregates?”
For every question above, prepare a second-level answer. The candidates who pass are not
the ones who memorized the first answer — they are the ones who kept going for another
thirty seconds and said something useful about why it matters.
Quick Quiz
Five questions to check whether the topics above have landed.
1. Which is the correct way to check for NULL?
2. What does COALESCE(NULL, NULL, 'default') return?