SQL Tutorial › Querying Data › NULL Values

SQL NULL Values

BeginnerLesson 15 of 6911 min read9 queries run on MySQL 8.4

NULL means no value: unknown, missing or not applicable. It is not zero and not an empty string, and it does not behave like a normal value. Comparisons with NULL are never true, arithmetic with NULL gives NULL, and aggregates skip it. Most “my query lost some rows” bugs come from NULLs.

Advertisement
In this lesson
  • Find missing values with IS NULL and IS NOT NULL
  • Why = NULL never matches
  • How NULL affects arithmetic, COUNT, DISTINCT and sorting
  • The MySQL NULL-safe operator <=>

What NULL means

In the sample data, NULL appears where a value does not exist:

  • the CEO has no department and no manager;
  • only sales staff have a commission;
  • David Fernandes has no email.
employees · 12 rows (some columns)
emp_idfirst_namedept_idmanager_idcommission
100MeeraNULLNULLNULL
101Asha1100NULL
102Rahul1101NULL
103Priya1101NULL
104Karan1102NULL
105Vikram210012000
106Neha21056000
107Arjun21054500
108Kavya3100NULL
109Rohan3108NULL
110Sneha4100NULL
111Amit1101NULL

IS NULL and IS NOT NULL

Employees without a commission:

SQL
SELECT first_name, commissionFROM employeesWHERE commission IS NULL;
ResultMySQL 8.4
first_namecommission
MeeraNULL
AshaNULL
RahulNULL
PriyaNULL
KaranNULL
KavyaNULL
RohanNULL
SnehaNULL
AmitNULL
9 rows in set
How this query works, step by step
  1. IS NULL is the only correct way to test for a missing value.
  2. Nine employees have no commission.

Employees with a commission:

SQL
SELECT first_name, commissionFROM employeesWHERE commission IS NOT NULL;
ResultMySQL 8.4
first_namecommission
Vikram12000
Neha6000
Arjun4500
3 rows in set

Why = NULL never works

SQL
SELECT first_nameFROM employeesWHERE commission = NULL;
ResultMySQL 8.4
Empty set
How this query works, step by step
  1. commission = NULL asks “is this value equal to an unknown value?”
  2. The answer is always UNKNOWN, never TRUE, even when commission itself is NULL.
  3. WHERE keeps only TRUE rows, so the result is an Empty set, with no error to warn you.

You can see the three-valued logic directly:

MySQL
SELECT NULL = NULL   AS equals,       NULL <> 1     AS not_equal,       NULL IS NULL  AS is_null,       NULL <=> NULL AS null_safe_equals;
ResultMySQL 8.4
equalsnot_equalis_nullnull_safe_equals
NULLNULL11
1 row in set
How this query works, step by step
  1. NULL = NULL and NULL <> 1 are both NULL (unknown).
  2. IS NULL returns a real true value: 1.
  3. <=> is MySQL’s NULL-safe equal: it treats two NULLs as equal and returns 1.

NULL in arithmetic

Any arithmetic with NULL gives NULL:

SQL
SELECT first_name, salary, commission,       salary + commission AS total_payFROM employeesWHERE dept_id IN (1, 2)ORDER BY emp_id;
ResultMySQL 8.4
first_namesalarycommissiontotal_pay
Asha125000NULLNULL
Rahul98000NULLNULL
Priya72000NULLNULL
Karan72000NULLNULL
Vikram10500012000117000
Neha54000600060000
Arjun48000450052500
Amit25000NULLNULL
8 rows in set
How this query works, step by step
  1. For the three sales staff, salary + commission is a real number.
  2. For the engineers, commission is NULL, so salary + NULL is NULL: their total pay disappears.
  3. Fix it with salary + IFNULL(commission, 0) or COALESCE(commission, 0); see IFNULL & COALESCE.
SQL
SELECT first_name, salary + COALESCE(commission, 0) AS total_payFROM employeesWHERE dept_id IN (1, 2)ORDER BY emp_id;
ResultMySQL 8.4
first_nametotal_pay
Asha125000
Rahul98000
Priya72000
Karan72000
Vikram117000
Neha60000
Arjun52500
Amit25000
8 rows in set

NULL in aggregates

Aggregate functions skip NULLs, except COUNT(*), which counts rows:

MySQL
SELECT COUNT(*)          AS all_rows,       COUNT(commission) AS with_commission,       SUM(commission)   AS total_commission,       AVG(commission)   AS avg_commissionFROM employees;
ResultMySQL 8.4
all_rowswith_commissiontotal_commissionavg_commission
123225007500.0000
1 row in set
How this query works, step by step
  1. COUNT(*) counts all 12 rows.
  2. COUNT(commission) counts only the 3 non-NULL values.
  3. SUM adds 12,000 + 6,000 + 4,500 = 22,500.
  4. AVG divides by 3 (the non-NULL count), not by 12: 7,500.
Watch out

If NULL really means zero in your data, AVG(column) gives the wrong answer. Use AVG(COALESCE(commission, 0)) to average over every row.

NULL in sorting and DISTINCT

  • In MySQL, NULLs sort first in ascending order and last in descending order (ORDER BY).
  • DISTINCT and GROUP BY treat all NULLs as one group (DISTINCT).
SQL
SELECT dept_id, COUNT(*) AS staffFROM employeesGROUP BY dept_idORDER BY dept_id;
ResultMySQL 8.4
dept_idstaff
NULL1
15
23
32
41
5 rows in set

NULL vs empty string vs zero

SQL
CREATE TABLE contacts (name VARCHAR(20), phone VARCHAR(15));INSERT INTO contacts VALUES ('Asha', '98450 12345'), ('Ravi', ''), ('Mona', NULL);SELECT name,       phone IS NULL  AS is_null,       phone = ''     AS is_empty,       LENGTH(phone)  AS lenFROM contacts;
ResultMySQL 8.4
mysql> CREATE TABLE contacts (name VARCHAR(20), phone VARCHAR(15))
Query OK, 0 rows affected
mysql> INSERT INTO contacts VALUES ('Asha', '98450 12345'), ('Ravi', ''), ('M…
Query OK, 3 rows affected
mysql> SELECT name,
nameis_nullis_emptylen
Asha0011
Ravi010
Mona1NULLNULL
3 rows in set
How this query works, step by step
  1. Ravi’s phone is an empty string: a value that has zero characters. = '' is true and the length is 0.
  2. Mona’s phone is NULL: no value. IS NULL is true, and both = '' and LENGTH() return NULL.
  3. Decide on one meaning for “no phone” in your design, usually NULL, and stick to it.

NOT NULL columns

If a column must always have a value, declare it NOT NULL when you create the table, and the database will refuse missing values (NOT NULL constraint).

Exercise: Customers without email

List the customers who have no email address.

Expected resultMySQL 8.4
customer_idcustomer_name
4David Fernandes
1 row in set
Hint 1

Use IS NULL, not = NULL.

Exercise: Total pay for everyone

Show every employee’s first_name and total_pay = salary plus commission, treating a missing commission as 0. Sort by total_pay descending.

Expected resultMySQL 8.4
first_nametotal_pay
Meera250000
Asha125000
Vikram117000
Rahul98000
Kavya88000
Sneha78000
Priya72000
Karan72000
Neha60000
Arjun52500
Rohan45000
Amit25000
12 rows in set
Hint 1

COALESCE(commission, 0) returns 0 when commission is NULL.

Quick quiz

1. Which condition finds rows where email is missing?

2. What is 100 + NULL?

3. A column has the values 10, NULL, 20. What is AVG(column)?

4. What does NULL <=> NULL return in MySQL?

NULL values FAQ

What is NULL in SQL?

NULL is a marker for a missing or unknown value. It is different from zero and from an empty string.

How do I check for NULL in SQL?

Use column IS NULL or column IS NOT NULL. Comparisons such as = NULL or <> NULL are never true.

Does COUNT include NULL values?

COUNT(*) counts all rows, including those with NULLs. COUNT(column) counts only rows where that column is not NULL.

How do I replace NULL with a default value?

Use COALESCE(column, default) (standard SQL) or IFNULL(column, default) in MySQL.