✏️ Explanatory Question

What is the difference between NULL, zero, and an empty string?

👁 11 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

12

What is the difference between NULL, zero, and an empty string?

Level: Basic — A classic conceptual trap; many candidates wrongly treat NULL as 0 or ''.

These three look similar but are completely different in MySQL:

  • NULL: Represents a missing, unknown, or undefined value. It is not a value at all — it means "no data".
  • Zero (0): A valid numeric value. It occupies storage and can be used in calculations.
  • Empty string (''): A valid string value of length 0. It is a known value — just with no characters.
Golden rule: NULL is never equal to anything — not even another NULL. That's why NULL = NULL returns NULL (unknown), and you must use IS NULL / IS NOT NULL to test for it.

Side-by-Side Comparison

Aspect NULL Zero (0) Empty String ('')
Meaning Unknown / missing Numeric value String with 0 length
Is it a value? No Yes Yes
Takes storage? Minimal (flag) Yes Yes
Used in math? Result becomes NULL Yes N/A
Tested with IS NULL = 0 = ''
LENGTH() returns NULL 1 (for '0') 0

The Big Gotcha: NULL in Comparisons

Wrong Way

  • WHERE column = NULL → returns nothing
  • NULL can't be compared with =

Right Way

  • WHERE column IS NULL → works correctly
  • Use IS NULL / IS NOT NULL

Quick Example

CREATE TABLE demo (
    id    INT,
    val   INT,
    txt   VARCHAR(20)
);

INSERT INTO demo VALUES (1, NULL, NULL);  -- unknown values
INSERT INTO demo VALUES (2, 0, '');       -- zero and empty string

-- Correct way to find NULLs
SELECT * FROM demo WHERE val IS NULL;     -- returns row 1

-- Math with NULL always yields NULL
SELECT 100 + NULL;   -- result: NULL
SELECT 100 + 0;      -- result: 100

-- Length comparison
SELECT LENGTH(NULL), LENGTH(''), LENGTH('0');  -- NULL, 0, 1
Interviewer tip: The one-liner they want — "NULL means unknown/missing (not a value), 0 is a valid number, and '' is a valid zero-length string. Always test NULL with IS NULL, never with =."