DataBolt

SQL LEARNING PLATFORM

5

CHAPTER 5

NULL - The Mystery Value

The concept that trips up almost every SQL beginner.

6

Learning Blocks

SQL

Interactive Queries

In This Chapter

Concepts you'll master

1

What NULL actually means (and what it does not mean)

2

IS NULL and IS NOT NULL — the only correct way to check for NULL

3

Why NULL = NULL is not TRUE in SQL

4

COALESCE to handle NULL gracefully

🌟 Think of it this way: NULL is the answer to 'what is the name of your 10th child?' — it is not 'nothing' or 'empty'. It is 'this value does not exist / is unknown.' SQL treats it completely differently from zero or empty string.

7.1 - Finding NULLs

SQL QUERY
SELECT name, manager_id 
FROM employees
WHERE manager_id IS NULL;

7.2 - The NULL = NULL Trap

❌ Common Mistake: WHERE manager_id = NULL will ALWAYS return 0 rows. NULL = NULL evaluates to NULL (unknown), not TRUE. This is defined by the SQL standard. Always use IS NULL or IS NOT NULL.

SQL QUERY
SELECT * FROM employees WHERE manager_id = NULL; ----> Returns 0
rows - WRONG
SELECT * FROM employees WHERE manager_id IS NULL; ----> Returns 2
rows - CORRECT

7.3 - COALESCE

SQL QUERY
SELECT name, COALESCE(CAST(manager_id AS CHAR), 'No Manager') AS 
       reports_to
FROM employees;

💡 Engineering Insight: COALESCE is invaluable in analytics. When calculating revenue per customer, a NULL (no orders) should show ₹0, not distort averages. COALESCE(SUM(total), 0) handles this cleanly.

Exercise

Practice your SQL skills

SQL EDITOR
Press semicolon (;) to auto-run query

Tasks

Complete all SQL challenges

1

Find employees who do not have a manager (manager_id is NULL).

Current Task
2

Find employees who have a manager (manager_id is NOT NULL).

3

Find employees who do not have a manager and are in Sales department.

Need help solving this task?