MySQL
MySQL

NULL & IS NULL

NULL represents a missing or unknown value it's fundamentally different from an empty string ("") or zero (0). NULL means "we have no value here at all," not "the value is blank" or "the value is zero." Because of this, you can't check for NULL with the equality operator you'd expect: WHERE age = NULL; does not work as intended, and will not match anything, even rows where age genuinely is NULL. This is a common early surprise for beginners. Instead, use IS NULL and IS NOT NULL: WHERE age IS NULL; correctly finds rows where age has no value. WHERE age IS NOT NULL; finds rows where a value has actually been provided. NULL also affects calculations: almost any calculation involving a NULL value results in NULL. 5 + NULL evaluates to NULL, not 5 NULL tends to "spread" through expressions it touches, which is worth remembering once you reach functions and aggregates later in this course.

Why this matters

  • ·NULL correctly represents genuinely missing information, instead of forcing a misleading fake value like 0 or an empty string.
  • ·IS NULL / IS NOT NULL let you specifically find or exclude incomplete records.
  • ·Understanding NULL early avoids a whole category of confusing bugs later, especially around calculations and joins.

Common mistakes

  • ·Using = NULL instead of IS NULL is one of the most common SQL mistakes it silently matches nothing.
  • ·Forgetting that NULL 'spreads' through calculations can produce unexpected NULL results in otherwise correct-looking queries.
  • ·Treating NULL and an empty string ('') as the same thing leads to incorrect filtering.
Your codePHP
Expected output
Empty set (0 rows)

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Frequently asked questions

NULL represents 'unknown,' and comparing anything to an unknown value (even with =) is itself unknown, not true so it never matches. IS NULL is the correct check.

Practice questions

Question 1

Write a query that returns students where age IS NOT NULL.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Question 2

Insert a student with a NULL age, then select students where age IS NULL.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Quick check

Just for this topic answer honestly, then check yourself.

1 / 3

Which correctly checks for a missing value?