CHAPTER 7
The concept that trips up almost every SQL beginner.
🌟 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.
Top-level employees with no manager
SELECT name, manager_id FROM employees WHERE manager_id IS NULL;
❌ 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
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.