An issue where Oracle uses NULL in not in

Source: Internet
Author: User

Http://www.linuxidc.com/Linux/2012-07/66212.htm

I used to summarize some of the questions about NULL in Oracle, and I happened to see another one in the process of reading today that we didn't notice before, which is the problem of using NULL in not.

Sql> select * FROM dept;

DEPTNO dname LOC
---------- -------------- -------------
Ten ACCOUNTING NEW YORK
DALLAS
SALES CHICAGO
OPERATIONS BOSTON

Sql> Select Deptno
2 from Dept
3 where Deptno in (10,50,null);

DEPTNO
----------
10

It is normal to see the use of in, even if there is null.

Sql> Select Deptno
2 from Dept
3 where Deptno not in (10,50);

DEPTNO
----------
20
30
40

It seems to fit our expectations.

Sql> Select Deptno
2 from Dept
3 where Deptno not in (10,50,null);

No rows selected

Why did you add a null before the 20, 30, 403 data is not displayed

In and not in are essentially or operations, so that when you calculate a logical OR, you deal with NULL in different ways, resulting in a different result.

Let's analyze the previous three statements

Sql> Select Deptno
2 from Dept
3 where Deptno in (10,50,null);

Here can be equivalent to where deptno=10 or deptno=50 or deptno=null, because is or connected, so long as one condition is true, the whole is fed to true. So Deptno's record for 10 shows up.

Sql> Select Deptno
2 from Dept
3 where Deptno not in (10,50,null);

Here is equivalent to where not (deptno=10 or deptno=50 or deptno=null), take the deptno=20 record for example.

Not (20=10 or 20=50 or 20=null)

Not (false or false or null)

NOT NULL

Null

(previously only known when the Where condition returns false is not true, now it seems to return NULL when it is not true, the following is a small experiment to prove this conjecture)

#####################

Sql> SELECT * FROM dept
2 where 1=null;

No rows selected

#####################

Sql> Select Deptno
2 from Dept
3 where Deptno not in (10,50);

Here is equivalent to where not (deptno=10 or deptno=50), still take deptno=20 for example.

Not (20=10 or 20=50)

Not (false or false)

Not false

True

Note: FALSE or null=null, while TRUE or null=true.

An issue where Oracle uses NULL in not in

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.