MariaDB Is Null

Summary: in this tutorial, you will learn how to use the MariaDB is null and is not null operators to check if a value is null or not.

Introduction to MariaDB is null operator

In MariaDB, null is a maker that indicates an unknown value, missing value, or inapplicable value.

null does not equal to any value, even itself. Therefore, you cannot use any comparison operator (=, >, <, >=, <=, !=) to compare null with another value.

To check if a value is null or not, you use the is null operator:

expression is null
Code language: SQL (Structured Query Language) (sql)

The is null returns true if the expression is null; otherwise, it returns false.

Note that in MariaDB, a value of 0 is considered false and non-zero values considered true.

The following example illustrates the result of the is null operator when comparing zero, empty string, and null with null:

select 
    0 is null,
    '' is null,
    null is null;
Code language: SQL (Structured Query Language) (sql)
mariadb is null example

To negate the is null operator, you use the is not null operator:

value is not null
Code language: SQL (Structured Query Language) (sql)

The expression returns true if the value is not null; otherwise, it returns false.

The following example shows the result of zero, empty string, and null when comparing with null using the is not null operator:

select 
    0 is not null,
    '' is not null,
    null is not null;
Code language: SQL (Structured Query Language) (sql)

MariaDB is null operator examples

Let’s use the countries table from the sample database to demonstrate the is null operator.

A) Using the MariaDB is null operator example

The following example uses the is null operator to find the countries that have unknown national days:

select 
    name, 
    national_day 
from 
    countries
where 
    national_day is null
order by
    name;
Code language: SQL (Structured Query Language) (sql)
mariadb is null operator example

B) Using the MariaDB is not null operator example

The following example uses the is not null operator to find countries that have national days recorded:

select 
    name, 
    national_day 
from 
    countries
where 
    national_day is not null
order by
    name;
Code language: SQL (Structured Query Language) (sql)
mariadb is not null example

In this tutorial, you have learned how to use the MariaDB is null operator to check if a value is null or not.

Was this tutorial helpful ?