Click here to Skip to main content
15,899,026 members
Articles / Programming Languages / SQL
Tip/Trick

EXCEPT & INTERSECT command in SQL Server

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
23 Dec 2011CPOL 32.3K   3   1
New Commands in SQL Server Ex: EXCEPT & INTERSECT
EXCEPT returns any distinct values from the left query that are not also found on the right query.

INTERSECT returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand.

The basic rules for combining the result sets of two queries that use EXCEPT or INTERSECT are the following:

The number and the order of the columns must be the same in all queries.

The data types must be compatible.

Differences:
Similar to the UNION command, INTERSECT also operates on two SQL statements. The difference is that, while UNION essentially acts as an OR operator (value is selected if it appears in either the first or the second statement), the INTERSECT command acts as an AND operator (value is selected only if it appears in both statements).

SQL
Except Example
SELECT * FROM EmployeeTable
EXCEPT
SELECT * FROM EmployeeTable
GO

Intersect Example
SELECT * FROM EmployeeTable
INTERSECT
SELECT * FROM EmployeeTable
GO

License

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


Written By
Web Developer Mahindra Logisoft Business Solution Limited, Chenn
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 it's unrealistic to use the same tab... Pin
Wühlmaus23-Dec-11 11:12
Wühlmaus23-Dec-11 11:12 

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.