# 理解NULL如何影響IN和EXITS語句

select 'true' from dual where 1 = null;

select 'true' from dual where 1 != null;

select 'true' from dual where 1 is null;

select 'true' from dual where null is null;

select 'true' from dual where null in (null);

select 'true' from dual where (null,null) in ((null,null));

select 'true' from dual where (1,null) in ((1,null));

select 'true' from dual where null = ANY (null);

select 'true' from dual where (null,null) = ANY ((null,null));

select 'true' from dual where (1,null) = ANY ((1,null));

select 'true' from dual where exists (select null from dual);

select 'true' from dual where exists (select 0 from dual where null is null);

IN和EXISTS在邏輯上是相同的。IN語句比較由子查詢返回的值，並在輸出查詢中過濾某些行。EXISTS語句比較行的值，並在子查詢中過濾某些行。對於NULL值的情況，行的結果是相同的。

selectename from emp where empno in (select mgr from emp);

selectename from emp e where exists (select 0 from emp where mgr = e.empno);

selectename from emp where empno not in (select mgr from emp);

selectename from emp e where not exists (select 0 from emp where mgr =

e.empno);

NOT IN語句實質上等同於使用=比較每一值，如果測試為FALSE或者NULL，結果為比較失敗。例如：

select 'true' from dual where 1 not in (null,2);

select 'true' from dual where 1 != null and 1 != 2;

select 'true' from dual where (1,2) not in ((2,3),(2,null));

select 'true' from dual where (1,null) not in ((1,2),(2,3));

select 'true' from dual where 1 not in (2,3);

select 'true' from dual where 1 != 2 and 1 != 3;

selectename from emp where empno not in (select mgr from emp where mgr is not

null);

selectename from emp where empno not in (select nvl(mgr,0) from emp);

Scott Stephens已經在Oracle公司工作大於13年之久，他的工作領域包括支援人員，電子商務，市場開發，以及軟體開發。

## Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD