NULL Handling: Functions
Module: SQL Fundamentals
-- WRONG: Returns nothing
SELECT * FROM employees WHERE manager_id = NULL;
-- CORRECT: Returns rows with NULL manager_id
SELECT * FROM employees WHERE manager_id IS NULL;
-- Find employees WITH managers
SELECT * FROM employees WHERE manager_id IS NOT NULL;
SELECT
salary,
bonus,
salary + COALESCE(bonus, 0) AS total_comp
FROM employees;
SELECT
COUNT(*) AS total_employees,
COUNT(bonus) AS employees_with_bonus,
AVG(bonus) AS avg_bonus
FROM employees;
-- Convert empty string to NULL
SELECT NULLIF(phone, '') AS phone_clean
FROM customers;
NULL is not a value - it means "unknown"
NULL = NULL returns FALSE (not TRUE)
Always use IS NULL or IS NOT NULL
NULL in calculations returns NULL
COALESCE returns first non-NULL value
COUNT(*) includes NULL, COUNT(column) excludes NULL
Core references in this topic include WHERE, =, != / <>. Learn what each one does, when to use it, and the execution or engine rules that matter.
WHERE
Filters rows before projection and sorting. It decides which rows continue through the query pipeline.
SELECT ... FROM table WHERE condition;
Most performance issues start with a weak WHERE clause or a missing supporting index.
=
Returns rows where the left and right values are exactly equal.
column = value
Use with exact matches. Do not use = NULL.
!= / <>
Returns rows where the compared values are not equal.
column <> value
SQL supports both <> and != in many engines, but <> is the portable form.
OR
Matches rows when at least one condition is TRUE.