SQL: Difference Between NOT IN and NOT EXISTS Operator

Опубликовано: 21 Август 2019
на канале: GoLearningPoint
4,807
49

#GoLearningPoint

NULL - Undefined value
This can not be compared with any other values not even with another NULL.

Difference Between Not IN and Not Exists Operator
###NOT IN Operator
The exception is where you have a NULL in the NOT IN subquery.This causes the NOT IN to always be false and
will not return any rows.NOT IN does not have the ability to compare the NULL values.

When using “NOT IN”, the query performs nested full table scans.


###Not Exists Operator
Not Exists is recommended is such cases.

Whereas for “NOT EXISTS”, query can use an index within the sub-query.


Select * From T1
where Col1 NOT IN(Select Col1 From T2 Where Col1 is NOT NULL);


Select * From T1
where Col1 NOT EXISTS(Select Col1 From T2 Where T1.Col1=T2.Col1);



Select * From MyTab;


/* List Employees who are not manager*/

/*Using NOT In*/
Select * From MyTab
Where ID NOT IN(Select MANAGERID From MyTab Where MANAGERID Is NOT NULL);


Select * From MyTab T1
Where NOT EXISTS(Select 1 From MyTab T2 Where T2.MANAGERID=T1.ID);


Смотрите видео SQL: Difference Between NOT IN and NOT EXISTS Operator онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь GoLearningPoint 21 Август 2019, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 4,80 раз и оно понравилось 4 людям.