SQL FOUNDATIONS:NULL Handling

Work safely with missing values using IS NULL and COALESCE.

SQL NULL Handling Explained with Examples + Practice Questions

What is NULL

NULL represents missing or unknown data.

It is not:

  • 0
  • empty string
  • false

It means no value exists.

When to Use NULL Handling

  • When data is missing
  • When filtering incomplete records
  • When working with joins and optional relationships

NULL-related questions are very common in SQL interviews.

Example Dataset

Table: employees

idnamedepartmentsalary
1JohnHR40000
2AliceIT60000
3BobNULL55000
4EmmaFinanceNULL
5DavidNULL45000

Checking NULL Values

Incorrect Way

sql
SELECT * FROM employees
WHERE department = NULL;

This will NOT work.

Correct Way

sql
SELECT * FROM employees
WHERE department IS NULL;

Checking NOT NULL

sql
SELECT * FROM employees
WHERE department IS NOT NULL;

NULL in Conditions

NULL cannot be compared using:

  • =
  • !=

Always use:

  • IS NULL
  • IS NOT NULL

Handling NULL with Functions

Using COALESCE (Replace NULL)

sql
SELECT name, COALESCE(department, 'Not Assigned') AS dept
FROM employees;

Replaces NULL with a default value.

Using IFNULL (MySQL specific)

sql
SELECT name, IFNULL(salary, 0) AS salary
FROM employees;

NULL in Aggregations

sql
SELECT AVG(salary) FROM employees;

NULL values are ignored. Only valid numbers are used.

NULL in COUNT

sql
SELECT COUNT(department) FROM employees;

Counts only non-null values.

sql
SELECT COUNT(*) FROM employees;

Counts all rows.

NULL with JOIN

sql
SELECT e.name
FROM employees e
LEFT JOIN departments d
ON e.department = d.name
WHERE d.name IS NULL;

Used to find unmatched records.

Key Concepts

  • NULL means missing data
  • Cannot use = or != with NULL
  • Use IS NULL / IS NOT NULL
  • Use COALESCE to replace NULL
  • NULL is ignored in most aggregations

Common Questions (FAQ)

What is NULL in SQL

NULL represents missing or unknown data.

Why = NULL does not work

Because NULL is not a value, it is a state.

How to replace NULL values

Use COALESCE or IFNULL.

Does COUNT include NULL

COUNT(column) ignores NULL, COUNT(*) includes all rows.

Internal Learning Links

  • Learn WHERE filtering
  • Learn JOINs
  • Learn Aggregations

Practice CTA

Practice NULL-based SQL problems on Schoolabe to handle real-world messy data.

NULL Handling Missions

Solve exercises in sequence to unlock the next mission.

1

Department is NULL

Retrieve employees where department is NULL.

Solve Mission
2

Salary is NOT NULL

Retrieve employees where salary is NOT NULL.

Locked
3

Replace NULL dept

Replace NULL department with 'Unknown'.

Locked
4

Replace NULL salary

Replace NULL salary with 0.

Locked
5

Count non-null dept

Count employees where department is NOT NULL.

Locked
6

Average salary

Find average salary ignoring NULL values.

Locked
7

Dept or salary NULL

Retrieve employees where department is NULL or salary is NULL.

Locked
8

Dept not null and salary > 50000

Retrieve employees where department is NOT NULL and salary > 50000.

Locked
9

Replace NULL salary 10000

Retrieve employees and replace NULL salary with 10000.

Locked
10

Unassigned employees via JOIN

Find employees who are not assigned to any department using JOIN logic.

Locked