Beware of null traps in SQL!

In the previous article, we used the analysis of the World Bank's global GDP and population data as a case, and reviewed the basic SQL query and analysis functions learned in the basic article.

From the beginning of this article, we will learn the content of the advanced article. The advanced part will introduce more complex multi-table queries, sub-queries, set operations, and various advanced data analysis techniques, which will enable us to truly appreciate the power of SQL data processing and analysis.

As the beginning of the advanced article, let's first discuss the null value problem in SQL, because the particularity of null values ​​makes us prone to some errors and problems.

Null and three-valued logic

The NULL value in SQL is a special value that represents missing/unknown data or inapplicable conditions . For example, if a user does not provide an email address when registering, then the user's email address is unknown; whether it is pregnant or not does not apply to male employees.

For most programming languages, there are only two cases for the results of logical operations: true (Ture) or false (False). But for SQL, there are three cases of logical operation results: true, false or unknown (Unknown) :

![threevalue](https://img-blog.csdnimg.cn/2019072917153780.png?x-oss-process=image/watermark,typeZmFuZ3poZW5naGVpdGk,shadow10,textaHR0cHM6Ly90b255ZG9uZy5ibG9nLmNzZG4ubmV0,size16,colorFFFFFF,t70#pic_center =178x)

For the WHERE condition in the SQL query, only data with a true result will be returned, and neither will the result be false or unknown.

Therefore, the logical operators AND, OR in SQL

Guess you like

Origin blog.csdn.net/horses/article/details/108729102
Recommended