SQL NOT IN Vs NOT EXIST Operator

The difference between NOT IN and NOT EXIST operators will be discussed in this post.


SQL NOT IN Operator

In the WHERE clause, the SQL NOT IN command allows you to specify several values.

You can think of it as a set of NOT EQUAL TO instructions separated by an OR condition.

The NOT IN command compares particular column values from the first table to other column values in the second table or a subquery, and returns all values from the first table that aren't found in the second table, without filtering for different values.

The NOT IN command considers NULL as a value and returns it. NULL can't be compared to anything else, including other NULL s.

As a result, if the result set being probed returns a NULL, a NOT IN operation will fail. The result of a NOT IN query in this scenario is o rows.

Example: Let's look at Books Table

id name price year domain
1 MySql Query Performance Tuning 100 2020 Administration
2 SQL For Microsoft Access 75 2017 Optimization
3 Pro MySql Administration 170 2017 Performance
4 Troubleshooting MYSQL 71.87 2016 Database

The following query execute Where ID is 2,3 is not exeucted. Because NOT IN Operator execute which value cannot be mentioned in query.

SELECT Year,Domain
FROM BookAdvance WHERE ID NOT IN ('ID',2,3);

In above query we cannot mentioned value of ID 1,4.

Output: The output of above query is

year domain
2020 Administration
2016 Database

SQL NOT EXIST Operator

The SQL NOT EXISTS command is used to verify that certain values in a subquery are present.

The subquery does not provide any data; instead, it returns TRUE or FALSE results based on the existence of the subquery variables.

NULL can't be compared to anything else, including other NULL s.

As a result, if the result set being probed returns a NULL , a NOT IN operation will fail.

Example: The following 2 table Book and Order are help to analyze the NOT EXIST Operator

BID BookName Price
B-1 Oracle Interview Questions 100
B-2 Securing MySql 70
OrderNo BID Qty OrderDate
1 B-2 9 2007/01/12 19:35:57
2 B-1 6 1995/01/25 11:35:52
3 B-1 1 2006/07/13 13:13:54
4 B-2 5 1995/06/26 18:13:49
SELECT BID
FROM Book
WHERE NOT EXISTS (SELECT BID FROM Order WHERE Order.BID >Book. BID);

In above query both table have id of BID table. So we compare both column in book and order table

Output: The output will be

bid
B-2

SQL Difference Between NOT IN And NOT EXIST Operator

Here , we discussed the distinguish in not in & not exist operator

NOT IN NOT EXIST
The exception is when the NOT IN sub query has a NULL. As a result, the NOT IN will always be false, and no rows will be returned. NOT IN can't compare NULL values because it doesn't have that capability. In such circumstances, NOT EXISTS is advised.
The query conducts nested full table scans when NOT IN is used. The NOT EXISTS query, on the other hand, can employ an index within the sub-query.
The WHERE clause of the SQL NOT IN command can have multiple values. You can think of it as a set of NOT EQUAL TO instructions separated by an OR condition. The NO IN command compares particular column values from the first table to other column values in the second table or a subquery, and returns all values from the first table that aren't found in the second table, without filtering for different values. The NOT IN command considers NULL as a value and returns it. The SQL NOT EXISTS command is used to verify that specified values in the given subquery exist. The subquery does not provide any data; instead, it returns TRUE or FALSE results based on the existence of the subquery variables.