Back to JOINS, UNION, NULL HANDLING

NULL HANDLING

Understand how to handle NULL values

15 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. Concept: NULL Values — NULL represents unknown or missing data and is distinct from zero or an empty string.
  2. ISNULL Boolean Check — The ISNULL function returns a boolean value indicating whether an expression is missing.
  3. ISNULL in Aggregates — ISNULL can substitute a value for NULLs before an aggregate function like SUM runs.
  4. COALESCE Function — COALESCE returns the first non-null expression found in a list of arguments.
  5. NULLIF Function — NULLIF returns NULL if its two arguments are equal, otherwise it returns the first argument.
  6. IFNULL Function — IFNULL provides a simple, two-argument way to replace a NULL value with a default.
PDF notes

Frequently asked questions

Why does NULL = NULL return false?

Because NULL represents an unknown value, comparing two unknown values results in an unknown outcome, which SQL treats as false in standard comparisons.

Should I use COALESCE or IFNULL?

Use `COALESCE` for maximum SQL standard compliance and portability, or when you need more than one fallback option. Use `IFNULL` for brevity in MySQL/SQLite.

How does NULLIF help with division?

You can use `100 / NULLIF(divisor, 0)` to ensure that if the divisor is zero, the result is NULL instead of causing a runtime division-by-zero error.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.