Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Handle NULL Values in MySQL Queries?

MySQL
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 30, 2026
7 min read

In MySQL, NULL means "no value" or "unknown", and it is not equal to anything, not even another NULL. To handle NULL values in queries, test with IS NULL and IS NOT NULL, replace them with COALESCE() or IFNULL(), and remember that comparisons with NULL give unknown rather than true or false. This guide explains mysql null values from the ground up.

Most NULL bugs come from using = NULL or <> value and getting an empty or incomplete result. The sections below show each rule with a runnable example.

Table of Contents

  • What NULL means in MySQL
  • Testing with IS NULL and IS NOT NULL
  • NULL in WHERE conditions and logic
  • Replacing NULL with COALESCE and IFNULL
  • NULL in sorting, aggregates and arithmetic
  • Frequently Asked Questions
    • Why does WHERE column = NULL return nothing?
    • What is the difference between NULL, 0 and an empty string?
    • Should I use COALESCE or IFNULL?
    • How do I insert or set a NULL value?
  • Summary
  • Related Articles

What NULL means in MySQL

A column that allows NULL can store "nothing here". That is different from 0 and different from an empty string ''. A missing phone number should be NULL, an order total of zero is 0, and a customer who chose to leave a nickname blank might be ''. Mixing these up makes queries return surprising results.

The examples use this table:

sql

Declare a column NOT NULL when a value is always required. That moves the rule into the database, so you handle fewer NULL cases later.

Testing with IS NULL and IS NOT NULL

Any ordinary comparison with NULL returns NULL (unknown), and WHERE keeps only rows where the condition is true:

sql
text

The correct tests are IS NULL and IS NOT NULL:

sql

Notice that Tom's empty string '' is not NULL, so he is in the second result. If your data mixes both, filter both: WHERE phone IS NULL OR phone = ''.

MySQL also has the null-safe equality operator <=>. It returns 1 when both sides are NULL and 0 when only one is, so a <=> b never returns NULL. It is handy for comparing two columns that may both be empty.

NULL in WHERE conditions and logic

NULL also spreads through AND, OR and NOT. This is called three-valued logic, with the values true, false and unknown:

ExpressionResult
TRUE AND NULLNULL
FALSE AND NULLFALSE
TRUE OR NULLTRUE
FALSE OR NULLNULL
NOT NULLNULL

The practical effect is that a negative filter silently drops NULL rows:

sql
text

Ravi has a NULL bonus, so bonus <> 0 is unknown for him and he is excluded. If you want everyone who does not have a zero bonus, including those with none, write WHERE bonus <> 0 OR bonus IS NULL, or WHERE NOT (bonus <=> 0).

The same trap appears with NOT IN. If the list or subquery contains even one NULL, x NOT IN (...) can never be true, and the query returns no rows. Use NOT EXISTS, or filter the NULL values out of the subquery.

Replacing NULL with COALESCE and IFNULL

To show a default instead of NULL, use COALESCE(), which returns its first non-NULL argument and is part of standard SQL. IFNULL(a, b) is the MySQL two-argument version:

sql
text

COALESCE accepts any number of arguments, so COALESCE(mobile, home, work, 'none') picks the first phone available. Its counterpart NULLIF(a, b) returns NULL when the two are equal. That is a safe way to avoid division by zero: total / NULLIF(count, 0) gives NULL instead of an error or warning.

NULL in sorting, aggregates and arithmetic

  • Arithmetic: any arithmetic with NULL gives NULL. price + bonus is NULL when bonus is NULL, so wrap it: price + COALESCE(bonus, 0).
  • String joining: CONCAT('a', NULL) is NULL. Use CONCAT_WS(' ', first, middle, last), which skips NULL arguments.
  • Sorting: in ORDER BY, MySQL treats NULL as the lowest value. It comes first with ASC and last with DESC. To push NULL to the end of an ascending sort, use ORDER BY bonus IS NULL, bonus.
  • Aggregates: SUM, AVG, MIN, MAX and COUNT(column) ignore NULL. COUNT(*) counts rows. AVG(bonus) divides by the number of non-NULL values, not the number of rows, which can surprise you.
  • Grouping: GROUP BY and DISTINCT treat all NULL values as one group.
  • Unique indexes: a UNIQUE index allows several NULL values in InnoDB, because NULL is never equal to another value.

Frequently Asked Questions

Why does WHERE column = NULL return nothing?

Because column = NULL evaluates to unknown for every row, and WHERE only keeps rows where the condition is true. Use IS NULL.

What is the difference between NULL, 0 and an empty string?

NULL is a missing or unknown value, 0 is a real number and '' is a real string of length zero. They compare differently and should be stored for different meanings.

Should I use COALESCE or IFNULL?

Both work in MySQL. COALESCE is standard SQL and takes more than two arguments, so it is the more portable choice.

How do I insert or set a NULL value?

Write the keyword without quotes: INSERT INTO employees (name, phone) VALUES ('Zed', NULL); or UPDATE employees SET phone = NULL WHERE id = 2;. The string 'NULL' is just text.

Summary

  • NULL means unknown, and comparisons with it return unknown, not true or false.
  • Test with IS NULL, IS NOT NULL or the null-safe operator <=>; never = NULL.
  • Negative filters and NOT IN drop NULL rows, so add OR col IS NULL or use NOT EXISTS.
  • COALESCE, IFNULL and NULLIF replace or create NULL values safely.
  • Aggregates skip NULL, NULL sorts first in ascending order, and arithmetic with NULL gives NULL.

Related Articles

  • How Do You Remove Duplicates From Results With DISTINCT?
  • How Do You Use COUNT, SUM, AVG, MIN and MAX in MySQL?
  • Learn MySQL: Complete Study Roadmap, Beginner to Advanced
Share