What's the difference between 'not in' and 'not exists'?

前端 未结 4 1850
野的像风
野的像风 2021-02-02 15:08

What\'s the difference between not in and not exists in an Oracle query?

When do I use not in? And not exist?

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 15:49

    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.

提交回复
热议问题