In MySQL, NULL represents an unknown or missing value, not zero, not an empty string, and not false. You must use special comparison operators and functions like IS NULL, IS NOT NULL, IFNULL(), and COALESCE() to work with NULL values correctly.
Understanding how NULL behaves in comparisons, conditions, and aggregate functions is essential for accurate queries and reports.
KEY IDEA: NULL = unknown / missing, not 0
0 (zero)'' (empty string)FALSE or 0.0IS NULL / IS NOT NULL.NULL usually produces NULL.SUM, AVG, COUNT(col) ignore NULL values.IFNULL() or COALESCE() to replace NULL with default values.Common patterns when working with NULL in MySQL:
NULL:
column_name IS NULLcolumn_name IS NOT NULLNULL with some default value:
IFNULL(expr, default_value)COALESCE(expr1, expr2, ..., exprN) – returns first non-NULL value.NULL:
ORDER BY will usually place NULLs first (ASC) or last (DESC), depending on MySQL version / settings.Suppose we have a employees table where some employees do not have a recorded commission:
-- Create employees table with NULL-able commission column
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
salary DECIMAL(10,2) NOT NULL,
commission DECIMAL(10,2) NULL
);
-- Insert sample rows; some employees have no commission
INSERT INTO employees (id, name, salary, commission) VALUES
(1, 'Alice', 50000.00, 2000.00),
(2, 'Bob', 45000.00, NULL),
(3, 'Carol', 60000.00, 3000.00),
(4, 'Dave', 40000.00, NULL);
-- Find employees where commission is missing (NULL)
SELECT id, name, salary, commission
FROM employees
WHERE commission IS NULL;
-- Find employees where commission is recorded (NOT NULL)
SELECT id, name, salary, commission
FROM employees
WHERE commission IS NOT NULL;
Query 1 (commission IS NULL):
Bob and Dave because their commission column has NULL.Query 2 (commission IS NOT NULL):
Alice and Carol where commission has a numeric value.= NULL Does Not WorkUsing = NULL or <> NULL does not produce the expected result, because comparisons with NULL yield UNKNOWN in three-valued logic (TRUE / FALSE / UNKNOWN).
-- ❌ Incorrect: this returns zero rows even if commission is NULL
SELECT id, name, commission
FROM employees
WHERE commission = NULL;
-- ✅ Correct: use IS NULL to test for NULL values
SELECT id, name, commission
FROM employees
WHERE commission IS NULL;
commission = NULL is never TRUE, so the query returns no rows.commission IS NULL is specially defined to check for NULL values correctly.Aggregate functions like SUM(), AVG(), and COUNT(col) ignore NULL values. Only COUNT(*) counts all rows.
-- Count all rows in the table (including NULL values)
SELECT COUNT(*) AS total_employees
FROM employees;
-- Count only rows where commission is NOT NULL
SELECT COUNT(commission) AS employees_with_commission
FROM employees;
-- Calculate total and average commission (NULL values ignored)
SELECT
SUM(commission) AS total_commission,
AVG(commission) AS average_commission
FROM employees;
COUNT(*) → counts all 4 employees.COUNT(commission) → counts only rows with non-NULL commission (2 employees).SUM(commission) and AVG(commission) ignore NULL commission values.To display or use a substitute value instead of NULL, use IFNULL() or COALESCE().
-- Show commission; if NULL, display 0 instead
SELECT
name,
IFNULL(commission, 0) AS commission_amount
FROM employees;
-- Use COALESCE to choose first non-NULL value
SELECT
name,
COALESCE(commission, 0) AS commission_amount
FROM employees;
-- Use COALESCE with multiple fallbacks (example pattern)
SELECT
name,
COALESCE(commission, salary * 0.05, 0) AS final_commission
FROM employees;
IFNULL(commission, 0) returns the commission if present, otherwise 0.COALESCE(commission, 0) behaves similarly for this two-argument case.COALESCE() returns the first non-NULL expression.IS NULL and IS NOT NULL to test for NULL values.NULL with 0 or an empty string. They have different meanings and behaviors.NULL only if “unknown / not applicable” is a valid state.IFNULL() or COALESCE() when displaying data to users to avoid showing blank or NULL values.col1 + col2; if one column is NULL, the result may be NULL.COUNT(*) for total rows and COUNT(column) specifically when you want to exclude NULL.students with columns: id, name, email, and phone. Allow phone to be NULL.phone as NULL and some have valid numbers.IS NULL).phone is NULL, display 'Not Provided' using IFNULL() or COALESCE().COUNT(phone), and compare it with COUNT(*).
-- 1. Create students table
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(20) NULL
);
-- 2. Insert sample data
INSERT INTO students (id, name, email, phone) VALUES
(1, 'John', 'john@example.com', '9991110001'),
(2, 'Mia', 'mia@example.com', NULL),
(3, 'Ravi', 'ravi@example.com', '8882220003'),
(4, 'Sara', 'sara@example.com', NULL),
(5, 'Liam', 'liam@example.com', '7773330005');
-- 3. Students without phone (NULL)
SELECT id, name, email, phone
FROM students
WHERE phone IS NULL;
-- 4. Display 'Not Provided' when phone is NULL
SELECT
id,
name,
email,
IFNULL(phone, 'Not Provided') AS phone_display
FROM students;
-- 5. Compare total rows vs rows with phone
SELECT
COUNT(*) AS total_students,
COUNT(phone) AS students_with_phone
FROM students;