Working with NULL

The most misunderstood concept in SQL — and how to handle it

Beginner 12 minNULLIS NULLIS NOT NULL

NULL Means Unknown

NULL means "unknown or missing." It is not zero, not an empty string, and not false. A customer without a phone number has NULL — it doesn't mean their number is blank. Here's what makes NULL tricky:

NULL ≠ NULL — The expression NULL = NULL evaluates to NULL (not true!). You cannot use = or != to test for NULL. You must use IS NULL or IS NOT NULL — these are the only operators that work correctly with NULL values.
NULL propagates everything5 + NULL → NULL. 'hello' || NULL → NULL. NULL > 10 → NULL. Any arithmetic or comparison involving NULL produces NULL. This is called three-valued logic: true, false, or unknown.
COALESCE is your fixCOALESCE(column, default) returns the first non-NULL argument in the list. Example: COALESCE(phone, 'No phone') returns the phone number if it exists, or the fallback string when it's NULL.
Knowledge Canvas

What NULL Really Means

NULL ≠ zero, NULL ≠ empty string

  • NULL = unknown / missing / not applicable
  • NULL is NOT a value — it's the absence of a value
  • Any comparison with NULL yields NULL (not TRUE/FALSE)
  • NULL propagates: 5 + NULL = NULL, 'hello' || NULL = NULL

The NULL Trap

The most common SQL bug

  • WHERE col = NULL → always empty (use IS NULL)
  • WHERE col != NULL → always empty (use IS NOT NULL)
  • NULL = NULL → NULL (not TRUE!)
  • NOT IN with NULLs → returns zero rows silently

NULL in Operations

How NULL propagates through expressions

  • Arithmetic: 5 + NULL = NULL
  • Concatenation: 'abc' || NULL = NULL
  • Comparison: NULL > 10 = NULL
  • Logical: TRUE AND NULL = NULL, TRUE OR NULL = TRUE
  • Aggregates: COUNT(*) includes NULLs, COUNT(col) skips them
  • COALESCE(col, fallback) replaces NULL with a default value

NULL-Safe Patterns

How to handle NULLs correctly

  • COALESCE(col, 0) for safe arithmetic
  • NULLIF(a, b) returns NULL if a = b
  • IS NULL / IS NOT NULL for filtering
  • IFNULL(col, default) — MySQL/SQLite shorthand

NULL Rules to Memorize

Never forget these

  • Use IS NULL, never = NULL
  • NULL in NOT IN = zero results
  • COUNT(*) counts NULLs, COUNT(col) doesn't
  • ORDER BY sorts NULLs to one end (DB-dependent)
  • GROUP BY treats all NULLs as one group
Syntax
Syntax Template
1-- Check for missing values
2WHERE column IS NULL
3
4-- Check for existing values
5WHERE column IS NOT NULL
6
7-- Replace NULLs with a default
8SELECT
9 COALESCE(column, 'default')
10FROM table;
IS NULLTrue when the value is missing/unknown
IS NOT NULLTrue when the value exists
COALESCE(a, b)Returns a if not NULL, otherwise b
Sample Data
reviews
12 rows
idproduct_idratingcomment
7001P1015Excellent mouse, very comfy
7002P1014NULL
7003P1013Good but battery drains fast
7004P1025Solid build, worth every rupee
7005P1024Spacious — fits two monitors
7006P1034NULL
7007P1045Just works, durable braided cable
7008P1055Great sound for the price
7009P1054Battery could be better
7010P1073NULL
7011P1085Good notebooks, smooth paper
7012P1095Premium feel, noise cancellation is real

Worked Example

Find reviews that have no comment, and show a default message instead.

SQL
1SELECT
2 id,
3 rating,
4 COALESCE(comment, '(no comment)') AS comment_display
5FROM reviews
6WHERE comment IS NULL;
IS NULL finds the reviews where comment is missing. COALESCE replaces the NULL with the text "(no comment)" in the output, making the result more readable.
Output
3 rows
idratingcomment_display
70024(no comment)
70064(no comment)
70103(no comment)
Common Mistakes

Using = to check for NULL

SQL
1SELECT
2 *
3FROM reviews
4WHERE comment = NULL;

This ALWAYS returns zero rows. NULL = NULL is not true — it evaluates to NULL, which is treated as false in WHERE.

Use IS NULL: WHERE comment IS NULL

Key Concepts

1NULL = NULL is NOT true — this is the #1 SQL gotcha
2Use IS NULL and IS NOT NULL — never = or !=
3NULL in any math or comparison produces NULL
4COALESCE returns the first non-NULL argument in the list

Pro Tip

NULL is the #1 source of silent SQL bugs. Queries that look correct can return wrong results because of unexpected NULL propagation.

When to Use

Finding missing email addresses, handling optional fields, replacing NULLs for display, dealing with incomplete data in reports.

Challenge

Solve the problem below

Find all reviews that DO have a comment. Show the product_id, rating, and comment.

Your Query