Skip to content
SQLSimplified

beginner

Understanding NULL

What NULL means in SQL, how it differs from zero or an empty string, and how to test for it correctly.

5 min read

Explanation

Sometimes a column simply has no value for a given row. Maybe the data was never collected, doesn't apply, or hasn't been entered yet. SQL represents this absence with a special marker called NULL.

In the employees table, most employees have a manager_id pointing to another employee. But someone at the top of the org chart, like a CEO, has no manager at all. That employee's manager_id is NULL, not zero, and not any employee's real id.

idfirst_namemanager_id
1AliceNULL
2Brian1
3Carla1

NULL isn't a value like 0 or "", it means "unknown" or "not applicable." This has a big consequence: you can't compare against NULL using the usual = operator.

= NULL never works

WHERE manager_id = NULL will never match any rows, even rows where manager_id really is NULL. Since NULL means "unknown," SQL can't determine whether an unknown value equals another unknown value, so the comparison is itself unknown, and unknown is treated as false.

Syntax

To correctly test for NULL, use the dedicated IS NULL and IS NOT NULL operators:

SELECT column_name
FROM table_name
WHERE column_name IS NULL;
SELECT column_name
FROM table_name
WHERE column_name IS NOT NULL;

You'll also encounter NULL when you want to substitute a default value instead of showing a blank. Later in this course, you'll meet the COALESCE function, which lets you replace NULL with a fallback value, for example showing "No Manager" instead of a blank cell. For now, just know that IS NULL and IS NOT NULL are the tools for filtering.

Interactive Example

Employees & Departments

Loading database engine...

Employees & Departments

Loading database engine...

Common Mistakes

  • Using = NULL or != NULL. Both silently return zero rows instead of raising an error, which makes this mistake easy to miss.
  • Assuming NULL means zero. A NULL salary is not the same as a salary of 0. One means "no data," the other means "the value is zero."
  • Forgetting NULL can appear in any column. Even columns that are usually filled in, like email or department_id, can contain NULL if the data is incomplete.

Best Practices

  • Always use IS NULL or IS NOT NULL when checking for missing values, never = or !=.
  • When designing or reading a schema, ask whether a column is allowed to be NULL and what that would mean for your query logic.
  • Be careful with NOT IN when the list might contain NULL, it can produce surprising empty results. This becomes more relevant once you start writing subqueries later in the course.

Practice Question

Using the playground above, write a query that returns the first_name and last_name of every employee who does have a manager (that is, whose manager_id is not missing).

Summary

NULL represents an unknown or missing value, it is not the same as zero or an empty string, and it can never be matched with =. Use IS NULL and IS NOT NULL to filter for missing or present values correctly.

FAQ

Search

Search lessons, functions, examples, and practice problems