Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Schedule Meeting

a

NULL comparisons in MariaDB, PostgreSQL, and SQLite

by | Sep 6, 2021 | MariaDB, MySQL, Other Databases, PostgreSQL

Need Help?  Click Here for Expert Support

Any comparison involving at least one NULL operand will return NULL. This is an implicit characteristics of NULL. But sometimes developers write too verbose conditions to handle NULL. For example when they want to consider NULLs equal they often write:

WHERE col = val OR (col IS NULL AND val IS NULL)

This condition is unnecessarily verbose and, most importantly, it’s possible that it will not use an index. Most DBMSs offer more specific ways to check values equality or inequality in a NULL-safe way.

Sometimes even Blixy is confused when it comes to NULL comparisons.

NULL-safe equality operators

Some operators are NULL-safe equalities / inequalities. They behave like normal equality operators, but they consider NULL as any other value. They consider NULL and NULL equal, NULL and FALSE different. These operators always return TRUE or FALSE, and never return NULL.

The following truth table shows this graphically.

 FALSETRUENULL
FALSE100
TRUE010
NULL001

NULL-safe operators are:

MariaDB / MySQL

  • WHERE a <=> b
  • WHERE NOT (a <=> b)

PostgreSQL

  • WHERE a IS NOT DISTINCT FROM b
  • WHERE a IS DISTINCT FROM b

SQLite

  • WHERE a IS b
  • WHERE a IS NOT b

Are you considering to always use this syntax instead of = to be sure you handle NULL correctly? Please don’t: NULL-safe conditions may prevent a query from using an index.

IS NOT operators

MariaDB, MySQL and PostgreSQL support the IS [NOT] TRUE and IS [NOT] FALSE operators. These are NULL-safe operators that consider TRUE, FALSE and NULL as regular, distinct values:

postgres=# SELECT NULL IS TRUE, NULL IS FALSE;
 ?column? | ?column?
----------+----------
 f        | f
(1 row)

postgres=# SELECT NULL IS NOT TRUE, NULL IS NOT FALSE;
 ?column? | ?column?
----------+----------
 t        | t
(1 row)

MariaDB / MySQL booleans

The astute reader probably knows that MariaDB and MySQL don’t support a real BOOL type. Actually, MariaDB/MySQL only support some synonyms:

BOOLEAN  = TINYINT UNSIGNED
BOOL     = TINYINT UNSIGNED
FALSE    = 0
TRUE     = 1

This has puzzling consequences, for example:

mysql> SELECT 2 = TRUE;
+----------+
| 2 = TRUE |
+----------+
|        0 |
+----------+

This is not a bug. On the contrary, it is perfectly consistent, because 2 <> 1. Nevertheless, from a user’s point of view, this result makes no sense.

Fortunately, IS TRUE and IS FALSE operators do what you would expect:

mysql> SELECT 2 IS TRUE, '' IS FALSE;
+-----------+-------------+
| 2 IS TRUE | '' IS FALSE |
+-----------+-------------+
|         1 |           1 |
+-----------+-------------+

MariaDB/MySQL also support IS [NOT] UNKNOWN, but that is just an alias for IS [NOT] NULL.

See also

Conclusions

To master advanced SQL and query optimisation, consider our SQL Optimisation Training for teams.

Federico Razzoli

Did you like this article?

All content in this blog is distributed under the CreativeCommons Attribution-ShareAlike 4.0 International license. You can use it for your needs and even modify it, but please refer to Vettabase and the author of the original post. Read more about the terms and conditions: https://creativecommons.org/licenses/by-sa/4.0/

About Federico Razzoli
Federico Razzoli is a database professional, with a preference for open source databases, who has been working with DBMSs since year 2000. In the past 20+ years, he served in a number of companies as a DBA, Database Engineer, Database Consultant and Software Developer. In 2016, Federico summarized his extensive experience with MariaDB in the “Mastering MariaDB” book published by Packt. Being an experienced database events speaker, Federico speaks at professional conferences and meetups and conducts database trainings. He is also a supporter and advocate of open source software. As the Director of Vettabase, Federico does business worldwide but prefers to do it from Scotland where he lives.

Recent Posts

MariaDB SEQUENCE: a Simple Approach to Synthetic Data

MariaDB SEQUENCE: a Simple Approach to Synthetic Data

MariaDB SEQUENCE is a storage engine that generates a sequence of positive integer numbers. However, in this article I will show you that it's easy to use SEQUENCE to generate more complex sequences, that are not necessarily numeric. This is a very convenient way to...

Stored Procedures in MariaDB: Smarter, Easier and More Powerful

Stored Procedures in MariaDB: Smarter, Easier and More Powerful

MariaDB supports stored procedures written in procedural SQL. Which essentially means "SQL with IF's and loops". Most DBMSs do the same, but every project supports different, incompatible syntaxes. MariaDB implemented Oracle's dialect, called PL/SQL. The base of the...

The EXPLAIN command and its variants in MariaDB and MySQL

The EXPLAIN command and its variants in MariaDB and MySQL

For a very long time, the EXPLAIN command remained the same in the MariaDB and MySQL world. It was good enough in most cases, but the community welcomed the implementation of extensions and variants. However, MariaDB and MySQL improved it in different ways. So, if...

Services

Need Help?  Click Here for Expert Support

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *