r/SQL • u/geminigamer369 • Jul 30 '26
Discussion What is the difference between using "Not In" vs using "not exists" in SQL
What is the difference between using "Not In" vs using "not exists" in SQL?
32
Upvotes
43
u/ydykmmdt Jul 30 '26
Beware the nulls.
44
u/Echidnal Jul 30 '26
NOT EXISTS checks whether matching rows exist in a subquery and evaluates to TRUE or FALSE for each row while NOT IN compares a value against a list of values to exclude matches. Also, they handle NULL values differently.
9
u/alegendmrwayne Jul 30 '26
I believe that NOT EXISTS is safer if there are potentially NULLs involved (at least in SQL Server)
Otherwise, performance-wise should be quite close
7
u/Thiondar ORACLE Jul 30 '26
Depends on the plan optimizer builds from the statement.
In modern Oracle databases this should result in the same plan.
Other databases may behave different.
7
u/Plane_Big_5912 Jul 30 '26
the practical difference is NULLs. if the subquery returns even one NULL, NOT IN matches nothing and it looks like your query is broken. NOT EXISTS doesnt have that problem which is why most people default to it. also on bigger tables the optimizer usually does better with not exists
3
u/TIMEBOMB_1412 Jul 30 '26
They handle null differently. Learnt that the hard way.
3
u/Rude_Issue_5972 Jul 31 '26
Not in cannot handle nulls. Hence useful to check only non null values
Not exists can handle nulls, hence useful in sub queries. As those can return null values.
4
u/Glum_Cheesecake9859 Jul 30 '26
Not exists is an if/else condition I.e. branching strategy.
Not in is a where condition to filter out rows.
1
u/kagato87 MS SQL Jul 30 '26
In some engines not exists tends to induce a different type of plan. It's not consistent, some queries it won't matter, but at scale exists is more reliably performant than in.
Both may compile to the same plan - the optimizer will generally handle that - but there are many moving parts and the cardinality estimator seems to struggle seeing past the in keyword.
And as others have mentioned, exists / not exists can have much more complex matching rules.
Something important to note - exists / not exists looks like a subquery. It is not. Subqueries have their risks on large scale data, while exists is actually join syntax. The semi join (exists) and anti join (not exists) are non-ansi, there is no "ANTI JOIN... ON..." But that's basically what it is.
1
u/Rumborack17 Jul 30 '26
Not in is used to filter data to not match certain values. The following query would return everything where column1 is not 1,2 or 3:
select * from testTabel where column1 not in (1,2,3)
Not exists is to used to test if something is already in the table/database. It gets used for example in "Alter Scripts", which add columns/tables/etc. or when you are unsure if you need an insert or an update. E.g.:
IF NOT EXISTS (select * from table where column1=1) BEGIN INSERT... END ElSE BEGIN UPDATE... END
1
u/IanYates82 Jul 30 '26
Can't do multiple columns with a NOT IN. Not Exists tends to read better, at least the way I tend to structure queries
-8
u/geminigamer369 Jul 30 '26 edited Jul 30 '26
SQL follows something called 3 value logic, It has True, False and Unknown also, unlike other languages which have just true and false.
Let's say tab1: Orderid, Custid 1,1 2,3 3,Null
tab2: Custid, Names 1,J 2,K 3,L 4,M In answering question which customers ordered if we use not in it returns nothing. Not In givesup at Null handling.
But not exists return J,L that is the difference.