使用 NOT IN SubQuery 时为 NULL
虽然在子查询中不使用 null 进行子查询处理,但我们需要消除 NULLS 以获得预期结果
create table #outertable (i int)
create table #innertable (i int)
insert into #outertable (i) values (1), (2),(3),(4), (5)
insert into #innertable (i) values (2), (3), (null)
select * from #outertable where i in (select i from #innertable)
--2
--3
--So far so good
select * from #outertable where i not in (select i from #innertable)
--Expectation here is to get 1,4,5 but it is not. It will get empty results because of the NULL it executes as {select * from #outertable where i not in (null)}
--To fix this
select * from #outertable where i not in (select i from #innertable where i is not null)
--you will get expected results
--1
--4
--5
虽然不使用 null 处理子查询,但要谨慎处理预期的输出