Handling missing information correctly is one of the most critical aspects of database engineering. In SQL, NULL is a special marker indicating that a data value does not exist or is unknown. Because NULL represents unknown data, it obeys unique mathematical rules called Three-Valued Logic (3VL).
In standard mathematics, (x = x) is always true. In SQL, because NULL means "unknown", one unknown value can never be asserted equal to another unknown value:
-- Evaluates to UNKNOWN (neither TRUE nor FALSE):
SELECT NULL = NULL; -- Result: NULL
-- INCORRECT: Will NEVER return any rows!
SELECT * FROM students WHERE phone_number = NULL;
-- CORRECT: Use IS NULL or IS NOT NULL
SELECT * FROM students WHERE phone_number IS NULL;
SELECT * FROM students WHERE phone_number IS NOT NULL;
When presenting data in user interfaces, you often want to show a friendly default message (like 'Not Provided' or 0) instead of an ugly NULL:
-- 1. ANSI Standard COALESCE: Returns first non-NULL value
SELECT
student_id,
first_name,
COALESCE(phone_number, emergency_contact, 'No Phone Available') AS contact_info
FROM students;
-- 2. MySQL IFNULL: Returns fallback if expression is NULL
SELECT
student_id,
first_name,
IFNULL(gpa, 0.00) AS safe_gpa
FROM students;
• COUNT(*): Counts all rows in the table, including rows with NULL attributes.
• COUNT(column_name): Ignores and skips NULL values, counting only populated entries.
• SUM, AVG, MIN, MAX: Automatically ignore NULL values during aggregation.
AVG(score) ignores NULLs: ((10 + 20 + 30) / 3 = 20).SUM(score)/COUNT(*) includes all 5 rows: (60 / 5 = 12). Understanding this distinction prevents massive accounting errors!
Next, let us learn how to manipulate text data using SQL String Functions!
NULL represents a missing, unknown, or inapplicable value. It does not mean zero (0), false, or an empty string (''); it represents the absence of any data.
Standard boolean logic has two states: TRUE and FALSE. SQL implements Three-Valued Logic with three possible states: TRUE, FALSE, and UNKNOWN. Any equality check with NULL (e.g., NULL = NULL) evaluates to UNKNOWN.
COALESCE is an ANSI SQL standard function that accepts multiple arguments and returns the first non-NULL value. IFNULL is a MySQL-specific function that accepts only two arguments.
Proceed to the next class in sequence to build production mastery of relational database architecture.
Continue to Class 26: String Functions → Previous: ← IN & BETWEEN | Full Syllabus
Why = NULL never matches any row in three-valued SQL logic.
TRUE, FALSE, and UNKNOWN truth tables.
How COUNT(*), COUNT(col), SUM, and AVG treat NULL values.
Using IS NULL on right table foreign keys in LEFT JOIN.