Question:Â What is the Difference Between EXCEPT operator vs. NOT IN
Answer:Â The EXCEPT operator returns all of the distinct rows from the query to the left of the EXCEPT operator when there are no matching rows in the right query. The EXCEPT operator is equivalent of the Left Anti Semi Join. EXCEPT operator works the same way NOT IN. EXCEPTS returns any distinct values from the query to the left of the EXCEPT operand that does not also return from the right query.
An example of EXCEPT operator is displayed along with an example of NOT IN. If you run both of the Query and compare the Execution plan it is exactly the same. EXCEPT and NOT IN does the same functions and have the same execution plan, but EXCEPT has a much simpler syntax. The row-by-row comparison provided by EXCEPT, combined with the number of rows being returned remaining consistent, provides compelling evidence that the re-factored query is correct. EXCEPT works with * as well as aliases.
Example: (Both of the scripts returns the same number of rows)
-- SQL SERVER 2005 Method USE AdventureWorks; GO SELECT ProductID FROM Production.Product EXCEPT SELECT ProductID FROM Production.WorkOrder; GO
-- SQL SERVER 2000 Method which works IN SQL SERVER 2005 USE AdventureWorks; GO SELECT ProductID FROM Production.Product WHERE ProductID NOT IN ( SELECT ProductID FROM Production.WorkOrder); GO
If you liked this video, please do not forget to subscribe to my YouTube Channel – SQL in Sixty Seconds.
Here are my few recent videos and I would like to know what is your feedback about them.
- One Query Many Plans – SQL in Sixty Seconds #171
- Best Value for Maximum Worker Threads – SQL in Sixty Seconds #170
- Copy Database – SQL in Sixty Seconds #169
- 9 SQL SERVER Performance Tuning Tips – SQL in Sixty Seconds #168
- Excel – Sum vs SubTotal – SQL in Sixty Seconds #167
- 3 Ways to Configure MAXDOP – SQL in Sixty Seconds #166
- Get Memory Details – SQL in Sixty Seconds #165
- Get CPU Details – SQL in Sixty Seconds #164
- Shutdown SQL Server Via T-SQL – SQL in Sixty Seconds #163
- SQL Server on Linux – SQL in Sixty Seconds 162
Reference:Â Pinal Dave (https://blog.sqlauthority.com)
7 Comments. Leave new
Hi Pinal,
Where can i view the data/schema of the tables used in the examples.
If it’s just one column, surely both EXCEPT and NOT IN are the same. However, if more columns are involved, then they are quite different. EXCEPT works on all columns of the result set whereas NOT IN would work on the column being referenced.
You forgot the biggest difference — what happens if there is a NULL in the data. In that case except works as you would expect, but not in won’t return any rows.
Hey Pinal,
As per your explanation , ” Except ” and ” Not In ” both are same ?
Read Nakul’s answer above.
Having issue where an EXCEPT query works as expected in the query window, but when I put the same code into a Stored Procedure it returns many more records. Basically it is returning records where NULL is in the record, where in the query window it was correctly not returning these records where both datasets being compared both had a null in that column. Can you tell me how to correctly set the settings for my stored procedures so it works correctly with EXCEPT in relation to how it handles NULLS? I’m using SQL Server 2012 and server is in Compatability Level: SQL Server 2012(110). Thanks!
Could you please explain the working of ‘AND’ & ‘OR’ Operator ?
I this AND work as Intersect & ‘OR’ work as UNION