NULL Handling
BeginnerNULL represents the absence of a value; SQL uses three-valued logic (TRUE/FALSE/UNKNOWN), so NULL comparisons require IS NULL/IS NOT NULL and functions like COALESCE and NULLIF.
Overview
NULL is not a value — it is a marker for unknown or missing data. SQL's three-valued logic means any comparison with NULL returns UNKNOWN, not TRUE or FALSE. This breaks standard programming intuition: NULL = NULL is UNKNOWN (not TRUE), and NULL != 5 is also UNKNOWN. The practical consequence is that WHERE col = NULL never matches any rows — you must use WHERE col IS NULL. Aggregate functions (SUM, AVG, COUNT(col)) ignore NULL values. COUNT(*) counts all rows; COUNT(col) counts non-NULL values in col. COALESCE returns the first non-NULL argument and is the standard way to substitute a default for NULL.
IS NULL, IS NOT NULL, and Three-Valued Logic
Three-valued logic means that boolean expressions in WHERE can evaluate to TRUE, FALSE, or UNKNOWN. Only TRUE rows pass the filter. Any arithmetic or comparison involving NULL produces NULL (which is UNKNOWN in a boolean context).
-- Correct: IS NULL / IS NOT NULL
SELECT id, full_name, manager_id
FROM employees
WHERE manager_id IS NULL; -- finds top-level employees (no manager)
-- Wrong: = NULL never matches anything
SELECT id FROM employees WHERE manager_id = NULL; -- returns 0 rows!
-- Three-valued logic demonstration
SELECT
NULL = NULL AS eq_null_null, -- UNKNOWN → false in WHERE
NULL IS NULL AS is_null, -- TRUE
NULL != 5 AS neq_five, -- UNKNOWN
NOT NULL AS not_null_val, -- UNKNOWN
TRUE AND NULL AS true_and_null, -- UNKNOWN
FALSE AND NULL AS false_and_null, -- FALSE (short-circuit)
TRUE OR NULL AS true_or_null; -- TRUE (short-circuit)
-- NULL in aggregate functions
SELECT
COUNT(*) AS total_rows, -- counts ALL rows
COUNT(commission) AS rows_with_commission,-- ignores NULLs
AVG(commission) AS avg_commission, -- ignores NULLs (not divides by total)
SUM(commission) AS total_commission -- NULLs treated as 0
FROM employees;COALESCE, NULLIF, IFNULL, NVL
COALESCE is ANSI-standard and works everywhere. IFNULL is MySQL-specific. NVL is Oracle-specific. NULLIF is the inverse of COALESCE — it returns NULL when two values are equal, useful to avoid division-by-zero.
-- COALESCE: return first non-NULL value (works in all databases)
SELECT
id,
COALESCE(phone, mobile, 'N/A') AS contact_number,
COALESCE(discount, 0) AS discount_amount
FROM orders;
-- NULLIF: returns NULL if two values are equal (prevents divide-by-zero)
SELECT
department_id,
SUM(sales) / NULLIF(COUNT(*), 0) AS avg_sales_per_head
FROM employees
GROUP BY department_id;
-- Without NULLIF: division by zero error if a dept has 0 employees
-- MySQL IFNULL (two-argument only; less flexible than COALESCE)
SELECT IFNULL(phone, 'N/A') FROM users;
-- Sorting NULLs intentionally
SELECT id, full_name, commission
FROM employees
ORDER BY COALESCE(commission, -1) DESC; -- NULLs sort as -1 (go last)
-- JPA: @Column(nullable=true) maps to nullable FK; use Optional<T> return type
// Optional<String> phone in entity → COALESCE handled at Java layer with Optional.orElse("N/A")Key Points to Remember
- 1Any comparison to NULL returns UNKNOWN — always use IS NULL / IS NOT NULL.
- 2Three-valued logic: WHERE filters include only TRUE rows; UNKNOWN rows are excluded.
- 3COUNT(*) counts all rows; COUNT(col) ignores NULLs in that column.
- 4COALESCE(a, b, c) returns the first non-NULL argument — ANSI standard.
- 5NULLIF(a, b) returns NULL when a = b — useful to prevent division-by-zero.
- 6AVG(col) divides by the count of non-NULL rows, not total rows.
Interview Questions
Sign in to ask AriaWhat is three-valued logic in SQL and why does it matter?
What is the difference between COUNT(*) and COUNT(column)?
How would you avoid a division-by-zero error in SQL?
Why does NOT IN return no rows when the subquery includes a NULL?
How does AVG handle NULL values — does it include them in the denominator?
Ask Aria about NULL Handling
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.