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. Null Functions OverviewNull functions are used to perform operations specifically on null values stored in database tables.
  2. Defining NULLNULL is a special value representing unknown or missing data, distinct from zero or blank strings.
  3. IS NULL FunctionIS NULL returns a boolean value indicating if an expression is null, often represented as 1 or 0.
  4. COALESCE FunctionCOALESCE returns the first non-null expression from a list of arguments provided to it.
  5. NULLIF FunctionNULLIF compares two expressions and returns NULL if they are equal, otherwise it returns the first expression.
  6. IFNULL FunctionIFNULL replaces a null value in the first argument with the specified value in the second argument.
  7. Database CompatibilityAlways check the specific database documentation to ensure a null function is supported before implementation.
PDF notes

Frequently asked questions

Is NULL the same as zero or a blank string?

No, NULL is a special value indicating missing or unknown data. It is neither zero nor blank.

How can I treat NULL as zero during aggregation?

Use a function like IFNULL(column, 0) or COALESCE(column, 0) inside the aggregate function, such as SUM().

What happens if all arguments in COALESCE are NULL?

If all expressions passed to COALESCE are NULL, the function itself will return NULL.

If NULLIF(A, B) returns NULL, how do I get B instead?

You must swap the parameters: NULLIF(B, A) will return B if A and B are different.