Skip to content
Learn Netverks

Lesson

Step 10/36 28% through track

null-handling

NULL handling

Last reviewed May 28, 2026 Content v20260528
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
beginner

This lesson

This lesson teaches NULL handling: the SQL patterns, schema habits, and query reasoning you need before advancing in SQL.

NULL is not false or zero—three-valued logic breaks COUNT and comparisons if you forget IS NULL.

You will apply NULL handling in contexts like: Postgres, MySQL, SQLite, warehouses, and ORMs that still expose SQL.

Copy SQL from each lesson into SQLite (sqlite3), DB Fiddle, or local Postgres—read result grids and row counts. The in-browser SQL lab (sql_sandbox) will run queries when the runner ships; until then, local clients are the practice path.

When you can explain the previous lesson's ideas without copying example queries verbatim.

NULL means unknown or missing—not zero, not empty string. Three-valued logic (TRUE, FALSE, UNKNOWN) makes NULL comparisons a frequent interview topic and a source of silent bugs.

Comparisons fail silently

SELECT name, email
FROM customers
WHERE email = NULL;  -- wrong: never matches

SELECT name, email
FROM customers
WHERE email IS NULL;

SELECT name
FROM customers
WHERE email IS NOT NULL;

Practice: Run in SQLite, DB Fiddle, or Postgres. Create the customers/orders sample from the relational-model lesson if needed.

COALESCE and IFNULL

SELECT name,
       COALESCE(email, 'no-email@placeholder.local') AS email_display
FROM customers;

COALESCE(a, b, ...) returns the first non-NULL argument—standard ANSI. SQLite also offers IFNULL.

Aggregates and NULL

SELECT COUNT(*) AS all_rows,
       COUNT(email) AS rows_with_email
FROM customers;

COUNT(column) ignores NULLs; COUNT(*) counts all rows.

Important interview questions and answers

  1. Q: NULL = NULL?
    A: Unknown—not TRUE. Use IS NULL for tests.
  2. Q: COALESCE vs CASE?
    A: COALESCE is concise for first-non-null; CASE handles richer conditional logic.

Self-check

  1. Which predicate finds rows where email is missing?
  2. Does COUNT(email) include NULL emails?

Pitfall: WHERE col = NULL never matches—use IS NULL.

Interview prep

NULL = NULL?

Unknown—not TRUE; use IS NULL.

COALESCE?

Returns first non-NULL argument.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • IS NULL why?
  • COALESCE when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump