MySQL NULL Value Handling
We already know that MySQL uses the SELECT command and WHERE clause to read data from a table, but when the provided query condition field is NULL, the command may not work properly.
In MySQL, NULL is used to represent missing or unknown data. Handling NULL values requires special care because it can lead to unexpected results in the database.
To handle this situation, MySQL provides three operators:
- IS NULL: When a column's value is NULL, this operator returns true.
- IS NOT NULL: When a column's value is not NULL, the operator returns true.
- <=>: A comparison operator (unlike the = operator) that returns true when both compared values are equal or both are NULL.
Conditional comparison operations on NULL are quite special. You cannot use = NULL or != NULL to find NULL values in a column.
In MySQL, comparing a NULL value with any other value (even NULL) always returns NULL, i.e., NULL = NULL returns NULL.
In MySQL, NULL is handled using the IS NULL and IS NOT NULL operators.
Note:
select * , columnName1+ifnull(columnName2,0) from tableName;columnName1 and columnName2 are of int type. When a value in columnName2 is null, columnName1+columnName2=null; ifnull(columnName2,0) converts null values in columnName2 to 0.
Common considerations and tips for handling NULL values in MySQL
- Check for NULL:
To check whether a column is NULL, you can use the IS NULL or IS NOT NULL condition.
SELECT * FROM employees WHERE department_id IS NULL;
SELECT * FROM employees WHERE department_id IS NOT NULL;
- Use the COALESCE function to handle NULL:
The COALESCE function can be used to replace NULL values. It accepts multiple arguments and returns the first non-NULL value in the argument list:
SELECT product_name, COALESCE(stock_quantity, 0) AS actual_quantity
FROM products;
In the above SQL statement, if the stock_quantity column is NULL, COALESCE will return 0.
- Use the IFNULL function to handle NULL:
The IFNULL function is a MySQL-specific version of COALESCE. It accepts two arguments and returns the second argument if the first is NULL.
SELECT product_name, IFNULL(stock_quantity, 0) AS actual_quantity
FROM products;
- NULL sorting:
When sorting with the ORDER BY clause, NULL values are placed at the end by default. If you want NULL values first, use ORDER BY column_name ASC NULLS FIRST, or conversely ORDER BY column_name DESC NULLS LAST.
SELECT product_name, price
FROM products
ORDER BY price ASC NULLS FIRST;
- Use <=> operator for NULL comparison:
SELECT * FROM employees WHERE commission <=> NULL;
- Note how aggregate functions handle NULL:
When using aggregate functions (such as COUNT, SUM, AVG), they ignore NULL values, so you may get unexpected results. If you want to treat NULL as 0, you can use COALESCE or IFNULL.
SELECT AVG(COALESCE(salary, 0)) AS avg_salary FROM employees;
This way, even if salary is NULL, the aggregate function will treat it as 0.
When handling NULL values, be especially careful to ensure the semantics of queries and operations meet expectations. When designing table structures, also consider the use cases and reasonableness of NULL values.
Using NULL values in the command prompt
In the following example, assume the table runoob_test_tbl in the RUNOOB database has two columns, runoob_author and runoob_count, with NULL values inserted in runoob_count.
Example
Try the following example:
Create the data table runoob_test_tbl
root
@
host
mysql -u root -p password;
Enter
password
:*******
mysql
use
RUNOOB
;
Database
changed
mysql
create
table
runoob_test_tbl
->
(
->
runoob_author
varchar
(
40
)
NOT
NULL
, ->
runoob_count
INT
->
)
;
Query
OK
,
0
rows
affected
(
0.05
sec
)
mysql
INSERT
INTO
runoob_test_tbl
(
runoob_author
,
runoob_count
)
values
(
'
RUNOOB
'
,
20
)
;
mysql
INSERT
INTO
runoob_test_tbl
(
runoob_author
,
runoob_count
)
values
(
'
Runoob Tutorial
'
,
NULL
)
;
mysql
INSERT
INTO
runoob_test_tbl
(
runoob_author
,
runoob_count
)
values
(
'
'
,
NULL
)
;
mysql
INSERT
INTO
runoob_test_tbl
(
runoob_author
,
runoob_count
)
values
(
'
FK
'
,
20
)
;
mysql
SELECT
from
runoob_test_tbl
; +---------------+--------------+ |
runoob_author
|
runoob_count
| +---------------+--------------+ |
RUNOOB
|
20
| | Runoob Tutorial |
NULL
| |
|
NULL
| |
FK
|
20
| +---------------+--------------+
4
rows
in
set
(
0.01
sec
)
In the following example you can see that the = and != operators do not work:
mysql
SELECT
FROM
runoob_test_tbl
WHERE
runoob_count
=
NULL
;
Empty
set
(
0.00
sec
)
mysql
SELECT
FROM
runoob_test_tbl
WHERE
runoob_count
!=
NULL
;
Empty
set
(
0.01
sec
)
To find whether a column in the table runoob_test_tbl is NULL, you must use IS NULL and IS NOT NULL, as in the following example:
mysql
SELECT
FROM
runoob_test_tbl
WHERE
runoob_count
IS
NULL
; +---------------+--------------+ |
runoob_author
|
runoob_count
| +---------------+--------------+ | Runoob Tutorial |
NULL
| |
|
NULL
| +---------------+--------------+
2
rows
in
set
(
0.01
sec
)
mysql
SELECT
from
runoob_test_tbl
WHERE
runoob_count
IS
NOT
NULL
; +---------------+--------------+ |
runoob_author
|
runoob_count
| +---------------+--------------+ |
RUNOOB
|
20
| |
FK
|
20
| +---------------+--------------+
2
rows
in
set
(
0.01
sec
)
Handling NULL values with PHP scripts
In a PHP script, you can use if...else statements to handle whether a variable is empty and generate corresponding conditional statements.
In the following example, PHP sets the $runoob_count variable and then uses it to compare with the runoob_count field in the table:
MySQL ORDER BY Test:
Runoob Tutorial IS NULL Test' ; echo '
| Author | Login Count |
| {$row['runoob_author']} | " . "{$row['runoob_count']} | " . "
The output is shown in the figure below: