Hello,
Which of these queries is the do better performance NOT IN vs NOT EXISTS?
and please give me an clear example if possible.
thanks
Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers to ask questions, answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Hi Maran Firstly
It’s not about the difference in performance only because they are not the same when dealing with nulls in the data.
for example, we want to check how many employees are not managers of other employees by using NOT IN
the result will be :
This means all employees are managers in this case.
let’s then take the same example but using NOT EXISTS
the result will be :
So here, out of 101 employees, there are 89, not managers.
The most crucial thing is NULL represented in the manager_id column for the first employee 101 (Steven King). So wherever null is there while using NOT IN or IN, the evaluation of the inner query will be either FALSE or NULL and will return no records.
So now, when it comes to performance, it depends on the amount of data that both subquery and the outer query returns. If they are small, then IN is typically more appropriate. And vise versa. But remember, we have assumed that there are no nulls in the subquery result.
Regards