SQL compound query condition (AND, OR, NOT) Processing of NULL values

Source: Internet
Author: User

The NULL value affects the query condition results, and the results are very subtle.

The following table lists the true values of AND, OR, and not in SQL.

Truth Table of table 1 AND

TRUE

FALSE

NULL

TRUE

TRUE

FALSE

NULL

FALSE

FALSE

FALSE

FALSE

NULL

NULL

FALSE

NULL

Table 2 OR truth table

TRUE

FALSE

NULL

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

NULL

NULL

TRUE

NULL

NULL

Truth Table of table 3 NOT

TRUE

FALSE

NULL

FALSE

TRUE

NULL

When two OR more query conditions are combined with AND, OR, and not, NOT has the highest priority, followed by AND, AND finally OR. Brackets are recommended to avoid ambiguity and ensure portability.

ABETWEENBANDC is equivalent to (A> = B)AND(A <= C). Therefore, based on the truth table, we can obtain the rule for processing NULL values in the BETWEEN clause.

Similarly,IN(B, C, D) is equivalent to (A = B)OR(A = C)OR(A = D), according to the truth table, as long as one of the three expressions is NULL, the returned result must be NULL.

Therefore,BETWEEN clause andIN clauseDoes not increase the expression capability of SQL statements.

SQL hasNULLValue test, namely:Field IS (NOT) NULLHowever, the returned results can only be TRUE or FALSE.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.