SQL NULL Values
SQL

SQL NULL Values — How to Handle Missing Data

CHAPTER 9 of 30 SQL Tutorial — Free Course on Neotech Navigators
Not every row in a database has a value for every column. When data is missing or unknown, SQL stores it as NULL. SQL NULL values are not zero, not an empty string — they mean no value at all. In this chapter, you will learn what NULL is, how to check for it, and how to handle it in your queries.

What Is NULL in SQL?

NULL represents a missing or unknown value in a database column. A field with NULL has no value — it was left blank when the row was created (perhaps during an INSERT INTO operation) or was explicitly set to NULL.

Value Meaning Example
NULL No value / unknown Employee has no phone number on file
0 A real numeric value Employee has zero bonuses
'' (empty string) A real text value (blank) Employee left the notes field empty

We will use this Employees table with some NULL values:

TABLE: Employees
+----+-----------+------------+--------+-----------+
| ID | Name      | Department | Salary | City      |
+----+-----------+------------+--------+-----------+
| 1  | Priya K.  | Sales      | 55000  | Mumbai    |
| 2  | John D.   | Marketing  | 62000  | London    |
| 3  | Arun M.   | IT         | 78000  | NULL      |
| 4  | Emma W.   | NULL       | 51000  | Sydney    |
| 5  | Ravi S.   | IT         | NULL   | Mumbai    |
| 6  | Sara L.   | HR         | 58000  | New York  |
+----+-----------+------------+--------+-----------+

Notice that Arun has no City, Emma has no Department, and Ravi has no Salary. These are all NULL values.

You Cannot Use = to Check for NULL

⚠️ Critical Rule

You cannot use = NULL or != NULL to check for NULL values. These comparisons always return no results because NULL is not equal to anything — not even itself. You must use IS NULL or IS NOT NULL instead. This is one of the most common mistakes in WHERE clause conditions.

SQL — WRONG vs CORRECT
-- ✗ WRONG: This returns NO results
SELECT * FROM Employees WHERE City = NULL;

-- ✓ CORRECT: Use IS NULL
SELECT * FROM Employees WHERE City IS NULL;

IS NULL — Find Rows with Missing Values

The IS NULL operator returns rows where the specified column has no value:

SQL
SELECT Name, City
FROM Employees
WHERE City IS NULL;
✓ OUTPUT
+-----------+------+
| Name      | City |
+-----------+------+
| Arun M.   | NULL |
+-----------+------+

Find employees with no department assigned:

SQL
SELECT Name, Department
FROM Employees
WHERE Department IS NULL;
✓ OUTPUT
+-----------+------------+
| Name      | Department |
+-----------+------------+
| Emma W.   | NULL       |
+-----------+------------+

IS NOT NULL — Find Rows with Values

The IS NOT NULL operator returns rows where the column does have a value:

SQL
SELECT Name, Salary
FROM Employees
WHERE Salary IS NOT NULL;
✓ OUTPUT
+-----------+--------+
| Name      | Salary |
+-----------+--------+
| Priya K.  | 55000  |
| John D.   | 62000  |
| Arun M.   | 78000  |
| Emma W.   | 51000  |
| Sara L.   | 58000  |
+-----------+--------+

Ravi S. is excluded because his Salary is NULL.

COALESCE — Replace NULL with a Default Value

The COALESCE() function returns the first non-NULL value from a list. It is commonly used to replace NULL with a meaningful default:

SQL
SELECT Name,
       COALESCE(City, 'Not Assigned') AS City
FROM Employees;
✓ OUTPUT
+-----------+--------------+
| Name      | City         |
+-----------+--------------+
| Priya K.  | Mumbai       |
| John D.   | London       |
| Arun M.   | Not Assigned |
| Emma W.   | Sydney       |
| Ravi S.   | Mumbai       |
| Sara L.   | New York     |
+-----------+--------------+

Arun’s NULL city is replaced with “Not Assigned” in the output. The actual table data is not changed — COALESCE only affects the display.

SQL — Replace NULL Salary with 0
SELECT Name,
       COALESCE(Salary, 0) AS Salary
FROM Employees;
✓ OUTPUT
+-----------+--------+
| Name      | Salary |
+-----------+--------+
| Priya K.  | 55000  |
| John D.   | 62000  |
| Arun M.   | 78000  |
| Emma W.   | 51000  |
| Ravi S.   | 0      |
| Sara L.   | 58000  |
+-----------+--------+

NULL in Calculations

Any arithmetic operation involving NULL returns NULL. This is a common trap:

SQL
SELECT Name,
       Salary,
       Salary * 12 AS AnnualSalary
FROM Employees;
✓ OUTPUT
+-----------+--------+--------------+
| Name      | Salary | AnnualSalary |
+-----------+--------+--------------+
| Priya K.  | 55000  | 660000       |
| John D.   | 62000  | 744000       |
| Arun M.   | 78000  | 936000       |
| Emma W.   | 51000  | 612000       |
| Ravi S.   | NULL   | NULL         |
| Sara L.   | 58000  | 696000       |
+-----------+--------+--------------+

💡 Fix NULL Calculations with COALESCE

To avoid NULL results in calculations, wrap the column with COALESCE: COALESCE(Salary, 0) * 12. This treats NULL salaries as 0 for the calculation, producing 0 instead of NULL.

NULL with COUNT

COUNT(*) counts all rows including those with NULL. But COUNT(column) only counts rows where that column is NOT NULL. You will learn more about COUNT and other aggregate functions in Chapter 14: SQL COUNT, AVG, SUM.

SQL
SELECT
  COUNT(*) AS TotalRows,
  COUNT(Salary) AS WithSalary,
  COUNT(Department) AS WithDept
FROM Employees;
✓ OUTPUT
+-----------+------------+----------+
| TotalRows | WithSalary | WithDept |
+-----------+------------+----------+
| 6         | 5          | 5        |
+-----------+------------+----------+

There are 6 total rows, but only 5 have a Salary value and 5 have a Department value.

For a complete reference of NULL handling functions, see the official MySQL NULL documentation. To permanently fix NULL values in your data, use the SQL UPDATE statement covered in the next chapter.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a query that shows all employees, replacing NULL departments with ‘Unassigned’ and NULL salaries with 0.

ANSWER
SELECT Name,
       COALESCE(Department, 'Unassigned') AS Department,
       COALESCE(Salary, 0) AS Salary,
       COALESCE(City, 'Unknown') AS City
FROM Employees;

Expected Output:

+-----------+------------+--------+-----------+
| Name      | Department | Salary | City      |
+-----------+------------+--------+-----------+
| Priya K.  | Sales      | 55000  | Mumbai    |
| John D.   | Marketing  | 62000  | London    |
| Arun M.   | IT         | 78000  | Unknown   |
| Emma W.   | Unassigned | 51000  | Sydney    |
| Ravi S.   | IT         | 0      | Mumbai    |
| Sara L.   | HR         | 58000  | New York  |
+-----------+------------+--------+-----------+

📝 What You Learned in This Chapter

  • NULL means no value / unknown — it is not zero or an empty string
  • Use IS NULL to find rows with missing values
  • Use IS NOT NULL to find rows that have values
  • Never use = NULL or != NULL — they do not work
  • COALESCE(column, default) replaces NULL with a default value in output
  • Any calculation with NULL returns NULL — use COALESCE to handle this
  • COUNT(*) counts all rows; COUNT(column) skips NULL values

Frequently Asked Questions

What is the difference between NULL and 0 in SQL?
NULL means no value exists — the data is missing or unknown. 0 is a real numeric value that means zero. For example, a NULL salary means the salary was never entered, while a salary of 0 means the employee is unpaid. They behave very differently in calculations and comparisons.
Why can’t I use = NULL in SQL?
Because NULL represents an unknown value, comparing anything to NULL with = always returns UNKNOWN (not TRUE or FALSE). SQL uses three-valued logic: TRUE, FALSE, and UNKNOWN. Only IS NULL and IS NOT NULL are designed to test for NULL values correctly.
What does COALESCE do in SQL?
COALESCE() returns the first non-NULL value from a list of arguments. It is used to replace NULL with a meaningful default. For example, COALESCE(Phone, Email, 'No Contact') returns the phone number if available, then the email, and finally ‘No Contact’ if both are NULL.
Does NULL affect aggregate functions like SUM and AVG?
Yes. Most aggregate functions ignore NULL values. SUM(Salary) adds only non-NULL salaries. AVG(Salary) calculates the average of non-NULL values only. COUNT(*) counts all rows, but COUNT(Salary) counts only rows where Salary is not NULL.
How do I update a NULL value in SQL?
Use an UPDATE statement with IS NULL in the WHERE clause: UPDATE Employees SET Department = 'Sales' WHERE Department IS NULL. This fills in the missing department for all employees who currently have NULL. You will learn UPDATE in detail in the next chapter.

📖 Chapter 9 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com