IS NULL, IS NOT NULL & Arithmetic With NULL Traps

⚡ Direct Answer & Key Takeaway

In SQL, `NULL` represents a missing, unknown, or inapplicable value—never numeric `0` or an empty string `''`. Because `NULL` is not a concrete value, `col = NULL` and `col <> NULL` always evaluate to `UNKNOWN`, returning zero rows; you must write `IS NULL` or `IS NOT NULL`. Any arithmetic operation involving `NULL`—including `NULL + 100`, `NULL - 0`, or even `NULL * 0`—propagates and yields `NULL`.

1. Why NULL Differs Fundamentally from Zero and Empty Strings

A zero (`0`) is a definite, measurable quantity that participates normally in arithmetic, sorting, and equality comparisons, whereas `NULL` signals absence of data. In an inventory or payroll table, setting `Discount = 0` means zero discount was explicitly granted, while `Discount = NULL` indicates the discount has not been assigned or does not apply. Similarly, an empty string `''` has length zero and satisfies `Code = ''`, whereas a `NULL` string fails both `Code = ''` and `Code <> ''`. CBSE board papers routinely insert one `0` row alongside two `NULL` rows in the same column to test whether students mistakenly treat them identically when filtering rows or predicting expression outputs.

  • `0` is an integer value; `''` is a zero-length string; `NULL` means unknown or missing.
  • `WHERE Discount = 0` returns `P102` and `P105`, never rows with `Discount = NULL`.
  • Always inspect sample table cells carefully so `0` and `NULL` are never lumped together.

2. Three-Valued Logic: IS NULL vs Broken Equality Checks

Relational databases evaluate `WHERE` predicates under three-valued logic: `TRUE`, `FALSE`, and `UNKNOWN`. Only rows where the condition evaluates to `TRUE` appear in the final output. Writing `WHERE Bonus = NULL` or `WHERE Bonus != NULL` yields `UNKNOWN` for every single row—even rows containing `NULL`—so the query returns an empty set without raising any syntax error. SQL provides dedicated predicates `IS NULL` and `IS NOT NULL` that return boolean `TRUE` or `FALSE` directly. Under boolean logic, `TRUE OR UNKNOWN` resolves to `TRUE`, whereas `FALSE OR UNKNOWN` and `TRUE AND UNKNOWN` resolve to `UNKNOWN`. Therefore, `WHERE Dept = 'Lab' OR Bonus IS NULL` safely preserves `'Lab'` rows even if their `Bonus` is non-null or missing.

  • `IS NULL` returns `TRUE` only when a cell has no value; `IS NOT NULL` is its exact boolean inverse.
  • `WHERE col = NULL` and `WHERE col <> NULL` always return 0 rows in standard SQL.
  • `NOT (UNKNOWN)` remains `UNKNOWN`, which explains why negated comparisons also skip `NULL` rows.

3. Arithmetic Propagation and IFNULL Handling in CBSE Queries

Whenever a `SELECT` expression performs `+`, `-`, `*`, `/`, or `%` with a `NULL` operand, the entire expression collapses to `NULL`. For instance, computing `BasePrice - Discount` returns `NULL` whenever `Discount` is `NULL`, even though intuition might expect `BasePrice` unchanged. Even `Quantity * 0` produces `NULL` if `Quantity` itself is `NULL`! To prevent unintended `NULL` outputs in reports, MySQL offers `IFNULL(expr, fallback)` and standard SQL provides `COALESCE(expr1, expr2, ...)`, so `BasePrice - IFNULL(Discount, 0)` substitutes `0` only during calculation without altering stored table data. Additionally, `NULL` values remain unaffected by `UPDATE ... SET Price = Price * 1.10` when `Price` is `NULL`, preserving missing state.

  • Arithmetic propagation rule: `anything + NULL = NULL`, `NULL * 0 = NULL`, `NULL / 2 = NULL`.
  • `IFNULL(Discount, 0)` temporarily substitutes `0` in `SELECT` without updating the stored table.
  • `CONCAT('A', NULL)` also returns `NULL` in MySQL unless `CONCAT_WS` or `IFNULL` is used.

4. IS NULL Filtering and Arithmetic Propagation Trap Demo

CREATE TABLE COMPONENTS (
    PartID VARCHAR(6) PRIMARY KEY,
    PartName VARCHAR(25),
    UnitCost INT,
    Discount INT,
    StockQty INT
);

INSERT INTO COMPONENTS VALUES
('P101', 'SSD Drive', 3200, 200, 15),
('P102', 'RAM Module', 1800, 0, 20),
('P103', 'Cooling Fan', 650, NULL, 12),
('P104', 'Power Unit', 2400, NULL, 8),
('P105', 'HDMI Cable', 350, 0, NULL),
('P106', 'USB Hub', 900, NULL, 10),
('P107', 'NVMe Enclosure', 1200, NULL, 5);

-- Query 1: IS NULL filter + arithmetic propagation vs IFNULL
SELECT PartID, PartName, UnitCost,
       UnitCost - Discount AS RawNet,
       UnitCost - IFNULL(Discount, 0) AS SafeNet
FROM COMPONENTS
WHERE Discount IS NULL;

-- Query 2: Broken equality check (always returns 0 rows)
SELECT PartID FROM COMPONENTS WHERE Discount = NULL;

Verified Console / Dry-Run Output:

-- Query 1 Output (4 rows; P102 and P105 have Discount = 0 so excluded):
PartID | PartName       | UnitCost | RawNet | SafeNet
P103   | Cooling Fan    | 650      | NULL   | 650
P104   | Power Unit     | 2400     | NULL   | 2400
P106   | USB Hub        | 900      | NULL   | 900
P107   | NVMe Enclosure | 1200     | NULL   | 1200

-- Query 2 Output:
Empty set (0 rows)

5. Result of Query 1 (IS NULL Filter & Arithmetic Propagation)

PartIDPartNameUnitCostRawNetSafeNet
P103Cooling Fan650NULL650
P104Power Unit2400NULL2400
P106USB Hub900NULL900
P107NVMe Enclosure1200NULL1200
🎯 1-on-1 Live Mentorship & Free Study Kit

Eliminate SQL NULL Traps with Free 1:1 Practice

Stop losing marks on SQL NULL traps and three-valued logic outputs in 4Achievers Junior (Grades 3-12, 1-on-1 & 1:4 Micro-Pods). Book a free 1:1 practice session now.

💬 WhatsApp Now
🔒 Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Writing `WHERE Discount = NULL` or `WHERE Discount != NULL` instead of `IS NULL` / `IS NOT NULL`.
  • Avoid Mistake: Assuming `UnitCost - NULL` equals `UnitCost` or `NULL * 0` equals `0`; both evaluate to `NULL`.
  • Avoid Mistake: Treating `Discount = 0` rows as matching `WHERE Discount IS NULL`; zero is a valid number, not `NULL`.
  • Avoid Mistake: Using `COUNT(Discount)` expecting missing rows to be counted; `COUNT(column)` skips `NULL` but counts `0`.
  • Examiner / Mentor Tip: In output questions showing `SELECT Salary + Bonus FROM ...`, write `NULL`—never just `Salary`—for any row where `Bonus` is `NULL`.
  • Examiner / Mentor Tip: Never write `= NULL` in a board answer script; examiners immediately deduct full marks for that condition.
  • Examiner / Mentor Tip: Remember that `PRIMARY KEY` columns automatically enforce `NOT NULL`, so they never contain `NULL`.

7. Interactive Concept Check Quiz

Question: Using the COMPONENTS table above, what does SELECT UnitCost - Discount FROM COMPONENTS WHERE PartID IN ('P102', 'P103'); output for P103 and P102?

8. Frequently Asked Questions

Is NULL equal to 0 or an empty string '' in SQL?
No. `NULL` represents an unknown state, not a numeric zero or blank string. `0 = 0` is `TRUE`, whereas `NULL = NULL` evaluates to `UNKNOWN`.
Why does WHERE Discount = NULL return zero rows without throwing an error?
Because SQL cannot confirm whether an unknown value equals anything, `col = NULL` resolves to `UNKNOWN`. Only rows evaluating strictly to `TRUE` pass a `WHERE` filter.
How do I prevent NULL from wiping out an arithmetic calculation?
Wrap nullable columns in `IFNULL(col, 0)` (MySQL) or `COALESCE(col, 0)` (standard SQL) inside the arithmetic expression, such as `UnitCost - IFNULL(Discount, 0)`.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class