MySQL Tutorial
In MySQL, NULL represents a missing or unknown value. It is not the same as zero (0) or an empty string ('').
If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value.
To test for NULL
values in MySQL, use the IS NULL
or IS NOT NULL
operators since NULL
cannot be compared with =
or !=
. Here are different ways to test for NULL
values:
The IS NULL
operator is used to test for empty values (NULL values).
The following SQL lists all students with a NULL value in the "city" field:
Below is a selection from the "Students" table in the school_db database:
student_id |
name |
gender |
city |
age |
score |
course_id |
|
phone_number |
---|---|---|---|---|---|---|---|---|
1 |
Alice |
Female |
Delhi |
20 |
85 |
101 |
NULL |
1234567890 |
2 |
Bob |
Male |
Mumbai |
22 |
75 |
NULL |
bob@email.com |
NULL |
3 |
Charlie |
Male |
Delhi |
21 |
95 |
102 |
NULL |
NULL |
4 |
David |
Male |
Bangalore |
23 |
65 |
101 |
david@email.com |
NULL |
5 |
Eve |
Female |
Mumbai |
20 |
80 |
103 |
NULL |
NULL |
6 |
Frank |
Male |
Delhi |
22 |
90 |
103 |
NULL |
NULL |
7 |
Alice |
Female |
Mumbai |
19 |
60 |
102 |
NULL |
NULL |
The IS NOT NULL
operator is used to test for non-empty values (NOT NULL values).
The following SQL lists all students with a value in the "city" field: