Click here to Skip to main content
12,633,519 members (29,849 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

103K views
14 bookmarked
Posted

EXCEPT vs NOT IN in SQL Server

, 10 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Except vs Not In in SQL Server

Introduction

"EXCEPT" operator was introduced in SQL SERVER 2005. This operator used to achieve Distinct and Not In queries. EXCEPT operator returns all distinct rows from left hand side table which does not exist in right hand side table.

On the other hand, "NOT IN" will return all rows from left hand side table which are not present in right hand side table but it will not remove duplicate rows from the result.

Example of EXCEPT vs NOT IN in SQL SERVER:

create table #tblsample (ProductId tinyint)
create table #tblsample2 (ProductId tinyint)

--insert values to tbl 1
insert into #tblsample values (1)
insert into #tblsample values (2)
insert into #tblsample values (3)
insert into #tblsample values (4)

--insert values to tbl 2
insert into #tblsample2 values (1)

--SELECT
select * from #tblsample
select * from #tblsample2

--USE EXCEPT
select * from #tblsample
except
select * from #tblsample2

--USE NOT IN
select * from #tblsample
where productid NOT IN(
select * from #tblsample2)

-- INSERT DUBLICATE VALUE
insert into #tblsample values (1)
insert into #tblsample values (2)

--SELECT
select * from #tblsample
--USE EXCEPT
PRINT 'EXCEPT RETURNS DISTINCT VALUES'
select * from #tblsample except select * from #tblsample2
--USE NOT IN
PRINT 'NOT IN RETURNS DUPLICATE VALUES IF ANY'
select * from #tblsample where productid NOT IN(select * from #tblsample2)

--USE DISTINCT + NOT IN = EXCEPT

select * from #tblsample
except
select productid from #tblsample2

--DROP TABLE
DROP TABLE #tblsample
DROP TABLE #tblsample2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

vinothharshad
Software Developer
India India
This is Vinoth from Coimbatore. Vinoth have been working as software developer since Jan 2011. Find Vino's other Interesting article in http://dotnettips4all.blogspot.in/

You may also be interested in...

Comments and Discussions

 
QuestionNULL value handling Pin
SMD11-Mar-16 3:10
memberSMD11-Mar-16 3:10 
QuestionClear and very helpful Pin
David Pierson9-Sep-15 15:05
memberDavid Pierson9-Sep-15 15:05 
Generalvery helpful article Pin
coolnavjot313-Jun-14 4:24
membercoolnavjot313-Jun-14 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 10 Jul 2013
Article Copyright 2013 by vinothharshad
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid