Skip to main content
SQL Fundamentals
CHAPTER 11 Beginner

Working with NULL Values

Updated: May 16, 2026
15 min read

# CHAPTER 11

Working with NULL Values

1. Introduction

In standard programming, if a user doesn't enter their phone number into a form, a developer might save it as a blank string "" or a zero 0. In SQL, a blank string is considered actual data (it is a string of length zero). A 0 is actual data (it is a number). But what happens when the data simply does not exist? We enter the void. In SQL, explicitly missing or unknown data is represented by the special marker NULL. In this chapter, we will learn how to handle NULL values and why they break standard mathematical logic.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define the concept of NULL in relational databases.
  • Understand why NULL cannot be compared using = or !=.
  • Filter for missing data using the IS NULL operator.
  • Filter out missing data using the IS NOT NULL operator.
  • Use the COALESCE() function to safely replace NULL values in output.

3. What exactly is NULL?

NULL is not a value. It is a *state*. It means "Unknown" or "Missing".
  • 0 means the value is zero.
  • "" means the value is empty text.
  • NULL means we have absolutely no idea what the value is.

*Example:* If a new employee is hired but hasn't negotiated their salary yet, their salary column is NULL.

4. The Mathematical Trap of NULL

Because NULL means "Unknown", you cannot perform standard math or logic on it. If you ask SQL, "Does Unknown equal 50?" SQL's answer is "Unknown".
sql
1234567
-- DANGER: This will return ZERO rows, even if John's phone is NULL!
SELECT * FROM users 
WHERE phone_number = NULL;

-- DANGER: This will also return ZERO rows!
SELECT * FROM users 
WHERE phone_number != NULL;

*(Rule: You can NEVER use = or != with NULL!)*

5. The IS NULL Operator

To specifically check if a column's state is missing, you must use the special IS NULL operator.
sql
1234567
-- Correct! Find all users who have not provided a phone number
SELECT name, email FROM users 
WHERE phone_number IS NULL;

-- Find orders that have not been shipped yet
SELECT * FROM orders 
WHERE shipped_date IS NULL;

6. The IS NOT NULL Operator

Conversely, if you are running a marketing campaign and only want to send texts to users who actually have a phone number on file, you use IS NOT NULL.
sql
123
-- Find all users who HAVE provided a valid phone number
SELECT name, phone_number FROM users 
WHERE phone_number IS NOT NULL;

7. Safely Handling NULLs in Math (COALESCE)

If you try to add numbers in SQL: 100 + NULL = NULL. Any math involving NULL instantly destroys the entire calculation and turns the result into NULL. If you are calculating a user's total compensation (Salary + Bonus), but they didn't get a bonus (it is NULL), the entire math fails.

To fix this, we use the incredibly powerful COALESCE() function. It takes a list of values and returns the first one that is NOT NULL.

sql
12345
-- If bonus is NULL, COALESCE replaces it with a 0 on the fly!
SELECT 
    name, 
    salary + COALESCE(bonus, 0) AS 'Total Compensation' 
FROM employees;

8. Mini Project: Auditing the Database

Let's run a data cleanliness audit on an E-Commerce system.
sql
12345678910111213
-- 1. Find all broken user profiles (missing an email address)
SELECT user_id, name FROM users 
WHERE email IS NULL;

-- 2. Find all products missing a price (so we don't accidentally give them away!)
SELECT product_id, product_name FROM products 
WHERE price IS NULL;

-- 3. Display the catalog, but if a product lacks a description, insert a default message
SELECT 
    product_name, 
    COALESCE(description, 'No description available for this item.') AS description 
FROM products;

9. Common Mistakes

  • Confusing NULL with Zero: If a customer's balance is 0, they owe you nothing. If their balance is NULL, the banking system doesn't know their account state. Never use them interchangeably.
  • Using IS NULL in INSERT Statements: When inserting data, you don't use IS NULL. You simply type the unquoted keyword NULL. Example: INSERT INTO users (name, phone) VALUES ('John', NULL);

10. Best Practices

  • NOT NULL Constraints: When architecting a database schema (Chapter 4 & 14), you can append NOT NULL to a column definition (e.g., email VARCHAR(100) NOT NULL). This physically prevents the database from ever accepting a row without an email, guaranteeing data integrity.

11. Exercises

  1. 1. Why does the query WHERE age = NULL fail to return results?
  1. 2. What SQL operator is explicitly required to filter out missing data?

12. SQL Challenges

Write a query to find all employees in the staff table who have a department assigned, but whose manager_id is missing.
sql
123
SELECT * FROM staff
WHERE department IS NOT NULL
AND manager_id IS NULL;

13. MCQ Quiz with Answers

Question 1

In a Relational Database, what does the state of NULL explicitly represent?

Question 2

If you execute a query containing WHERE phonenumber = NULL, what is the architectural result?

14. Interview Questions

  • Q: Explain the philosophical difference between storing an empty string "" in a VARCHAR column versus allowing that column to default to NULL.
  • Q: A financial query calculating basesalary + annualbonus is returning blank rows for several employees. You discover those employees did not receive an annualbonus this year. Explain the mechanical trap of NULL math causing this error, and how the COALESCE() function solves it.

15. FAQs

Q: Do NULL values take up hard drive space? A: In modern databases like PostgreSQL and MySQL, NULL values take up virtually zero physical space on the hard drive (often just a single bit to flip a flag), making them highly efficient for optional data.

16. Summary

You have mastered the void. By understanding that NULL is an undefined state that destroys mathematical equality, and by correctly implementing the IS NULL operator and the COALESCE() function, you guarantee that missing data will never crash your application logic or corrupt your financial reporting.

17. Next Chapter Recommendation

We know how to calculate simple math on a single row. But what if the CEO wants to know the TOTAL sum of all salaries combined, or the AVERAGE age of all users? In Chapter 12: Aggregate Functions in SQL, we will unlock the mathematical heavy lifting of database analytics.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·