What\'s the difference between not in
and not exists
in an Oracle query?
When do I use not in
? And not exist
?
Not in is testing for the present of an element in a set of elements, so it is simpler.
Not exists can handle more complicated queries, including grouping (eg having sum(x)=z or having count(*)>3), results with multiple conditions (eg matching multiple elements), and can take advantage of indexes.
In some situations not in is easier to do than not exists. I generally find this is where I am testing for the value of a key field in set of values.
As a rule of the thumb, I prefer not exists as it covers a lot more situations than not in. Not exists can be used for every situation that not in is used for, but not the reverse.