I asked myself whether to use <> ALLor NOT IN. Take a look at these two sample queries:
<> ALL
NOT IN
SELECT PersonId FROM tabPerson WHERE PersonId <> ALL(SELECT ParentId FROM tabPerson)
SELECT PersonId FROM tabPerson WHERE PersonId NOT IN(SELECT ParentId FROM tabPerson)
Two queries return exactly the same results.
Now I wonder what are the main differences between <> ALLand NOT IN. Does anyone have an idea?
Both queries will produce exactly the same results. Even if the column requested in the subquery contains NULL values, this will not make any difference.
, .
, - / . NOT IN . ALL ( ANY SOME) .
ALL
ANY
SOME
. MS Technet , , . - BTW: , .
:
DECLARE @t1 TABLE (a INT) INSERT INTO @t1 VALUES (1), (2) DECLARE @t2 TABLE (b INT) INSERT INTO @t2 VALUES (2) SELECT * FROM @t1 WHERE a <> ALL(SELECT b FROM @t2) SELECT * FROM @t1 WHERE a NOT IN (SELECT b FROM @t2)
NULL:
NULL
INSERT INTO @t2(b) VALUES (NULL) SELECT * FROM @t1 WHERE a <> ALL(SELECT b FROM @t2) SELECT * FROM @t1 WHERE a NOT IN (SELECT b FROM @t2)