w3resource

MySQL IS NULL

IS NULL

MySQL IS NULL operator tests whether a value is NULL. If satisfied, then returns 1 otherwise returns 0.

Syntax:

IS NULL

MySQL Version: 8.0

Example : MySQL IS NULL

In the following MySQL statement, it is checked whether 2, 0 and NULL are NULL, using IS NULL operator.

Code:


-- This query checks if the value 2 is NULL.
SELECT 2 IS NULL,
-- This part returns 0 (false) because 2 is not NULL.
-- This query checks if the value 0 is NULL.
0 IS NULL,
-- This part returns 0 (false) because 0 is not NULL.
-- This query checks if the value NULL is NULL.
NULL IS NULL;
-- This part returns 1 (true) because NULL is indeed NULL.

Explanation:

  • The purpose of this SQL query is to evaluate whether certain values are NULL and return the corresponding boolean result (1 for true, 0 for false).

  • SELECT 2 IS NULL, 0 IS NULL, NULL IS NULL: This part of the query performs three boolean checks using the IS NULL operator.

    • 2 IS NULL: This checks if the value 2 is NULL. Since 2 is a specific, non-null value, this expression evaluates to 0 (false).

    • 0 IS NULL: This checks if the value 0 is NULL. Since 0 is a specific, non-null value, this expression also evaluates to 0 (false).

    • NULL IS NULL: This checks if the value NULL is NULL. Since NULL represents a missing or undefined value, this expression evaluates to 1 (true).

  • The query will return a row with the results of these evaluations:

    • The first column will be 0 because 2 is not NULL.

    • The second column will be 0 because 0 is not NULL.

    • The third column will be 1 because NULL is indeed NULL.

Output:

mysql> SELECT 2 IS NULL, 0 IS NULL, NULL IS NULL;
+-----------+-----------+--------------+
| 2 IS NULL | 0 IS NULL | NULL IS NULL |
+-----------+-----------+--------------+
|         0 |         0 |            1 | 
+-----------+-----------+--------------+
1 row in set (0.00 sec)

Slideshow of MySQL Comparison Function and Operators

Previous: IS NOT
Next: IS



Follow us on Facebook and Twitter for latest update.