-
Hajipur, Bihar, 844101
In MySQL, NULL represents a missing or undefined value in a table. It is different from 0 or an empty string. Working with NULLs requires special handling because standard operators like = or != do not work as expected with NULL.
MySQL provides several NULL functions to detect, replace, or handle NULL values in queries. These functions are essential for data integrity, reporting, and calculations.
The simplest way to check for NULL values is using IS NULL or IS NOT NULL:
SELECT name, age
FROM students
WHERE age IS NULL;
Returns students whose age is unknown or not entered.
SELECT name, age
FROM students
WHERE age IS NOT NULL;
Returns students whose age is known.
= and != cannot be used with NULL.
Always use IS NULL or IS NOT NULL for proper checks.
The IFNULL() function replaces NULL with a specified value. Syntax:
IFNULL(expression, replacement_value)
expression is the column or value to check.
replacement_value is returned if expression is NULL; otherwise, expression is returned.
SELECT name, IFNULL(age, 18) AS Age
FROM students;
If a student’s age is NULL, it will display 18 instead.
Useful for default values in reports.
The COALESCE() function returns the first non-NULL value from a list of expressions:
COALESCE(expression1, expression2, ..., expressionN)
SELECT name, COALESCE(age, default_age, 18) AS Age
FROM students;
Returns age if not NULL, otherwise default_age, otherwise 18.
COALESCE is more flexible than IFNULL because it can handle multiple fallback values.
The NULLIF() function returns NULL if two expressions are equal; otherwise, it returns the first expression. Syntax:
NULLIF(expression1, expression2)
SELECT name, NULLIF(score, 0) AS Score
FROM students;
If score is 0, it returns NULL.
Useful for avoiding division by zero or marking specific values as missing.
MySQL aggregate functions like SUM, AVG, COUNT, MAX, MIN handle NULL differently:
COUNT(column_name) ignores NULL values.
COUNT(*) counts all rows regardless of NULLs.
SUM(column_name) and AVG(column_name) ignore NULLs.
MAX and MIN skip NULLs.
SELECT COUNT(age) AS AgeCount, AVG(age) AS AverageAge
FROM students;
AgeCount counts only non-NULL ages.
AverageAge is calculated ignoring NULL values.
SELECT name,
CASE
WHEN age IS NULL THEN 'Unknown'
ELSE 'Known'
END AS AgeStatus
FROM students;
Classifies students based on whether their age is NULL or not.
SELECT name, IF(age IS NULL, 18, age) AS Age
FROM students;
Similar to IFNULL but uses conditional logic.
Default value for NULL age:
SELECT name, IFNULL(age, 18) AS Age
FROM students;
First non-NULL value from multiple columns:
SELECT name, COALESCE(age, default_age, 18) AS Age
FROM students;
Mark zero scores as NULL:
SELECT name, NULLIF(score, 0) AS Score
FROM students;
Filter students with unknown age:
SELECT name
FROM students
WHERE age IS NULL;
Count students with known ages:
SELECT COUNT(age) AS AgeCount
FROM students;
Ignores NULL values.
NULL is not equal to 0 or an empty string.
Use IS NULL or IS NOT NULL to check for NULL.
IFNULL() replaces NULL with a single fallback value.
COALESCE() returns the first non-NULL value from a list.
NULLIF() converts specific values to NULL.
Aggregates like SUM, AVG, MAX, MIN ignore NULLs, while COUNT(*) counts all rows.
Proper handling of NULLs prevents errors and incorrect calculations.
NULL represents missing or undefined data.
Use IS NULL/IS NOT NULL to check for NULL values.
IFNULL() and COALESCE() replace NULLs with fallback values.
NULLIF() helps mark specific values as NULL.
Aggregates automatically ignore NULLs unless COUNT(*) is used.
Proper handling of NULL ensures accurate reporting, calculations, and data integrity.
Understanding MySQL null functions is essential for cleaning, transforming, and analyzing data, especially when dealing with incomplete or optional information.
Retrieve all students and display age as 18 if it is NULL using IFNULL().
Retrieve student names and the first non-NULL value among age, default_age, or 18 using COALESCE().
Retrieve scores but convert 0 values to NULL using NULLIF().
Select students whose age is NULL.
Select students whose age is NOT NULL.
Count the number of students whose age is not NULL.
Calculate the average age of students, ignoring NULL values.
Select students and display 'Unknown' for age if it is NULL using CASE.
Select students and display age as is if not NULL, otherwise 18, using IF().
Select total sum of scores, ensuring NULL scores are ignored in calculation.