SQL NULL values
✅ What NULL Means
NULL = unknown / missing / not applicable value
Important:
-
NULLis not zero. -
NULLis not an empty string. -
NULLcannot be compared with=or!=.
✅ How to Check for NULL
Correct:
Not allowed (ALWAYS false):
✅ Handling NULL in Expressions
COALESCE (ANSI SQL)
Returns the first non-NULL value.
IFNULL (MySQL)
NVL (Oracle)
✅ NULL in Aggregations
Aggregates skip NULLs:
✅ NULL and JOINs
If a foreign key column contains NULL, the row will not match in an INNER JOIN.
Rows with o.customer_id IS NULL will appear with customer fields also NULL.
✅ NULL in Constraints
Allow NULL (default)
Disallow NULL
🔥 Common Best Practices
✔ Use NULL to represent missing/unknown data
✔ Use NOT NULL for required columns
✔ Index columns with many NULLs carefully (engine-specific)
✔ Use COALESCE to substitute defaults safely
