DataBolt

All Lessons

Introduction to SQL
SQL Lesson 1: Your Sample Database
SQL Lesson 2: SELECT — Reading data
SQL Lesson 3: WHERE — Filtering rows
SQL Lesson 4: AND, OR, NOT
SQL Lesson 5: BETWEEN, IN, LIKE
SQL Lesson 6: NULL — The Mystery Value
SQL Lesson 7: ORDER BY
SQL Lesson 8: LIMIT & OFFSET
SQL Lesson 9: Aggregate Functions
SQL Lesson 10: GROUP BY
SQL Lesson 11: HAVING
SQL Lesson 12: INNER JOINs
SQL Lesson 13: LEFT JOINs
SQL Lesson 14: RIGHT JOINs
SQL Lesson 15: SELF JOINs
SQL Lesson 16: UNION JOINs
SQL Lesson 17: Joining Multiple Tables
SQL Lesson 18: Subqueries
SQL Lesson 19: CTEs (WITH)
SQL Lesson 20: CASE Statements
SQL Lesson 21: Window Functions
SQL Lesson 22: String Functions
SQL Lesson 23: Date & Time Functions
SQL Lesson 24: INSERT, UPDATE, DELETE
SQL Lesson 25: CREATE TABLE & DDL
SQL Lesson 26: Indexes & Performance
SQL Lesson 27: Transactions & ACID
SQL Lesson 28: SQL Execution Order
SQL Lesson 29: 50 Practice Problems

CHAPTER 7

NULL - The Mystery Value

The concept that trips up almost every SQL beginner.

IN THIS CHAPTER

  • What NULL actually means (and what it does not mean)
  • IS NULL and IS NOT NULL — the only correct way to check for NULL
  • Why NULL = NULL is not TRUE in SQL
  • 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

Top-level employees with no manager

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.

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

Show 'No Manager' where manager_id is NULL

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 👇

Exercise:

Tasks

1.👉Find employees who do not have a manager (manager_id is NULL).
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.
Stuck? Read this task's