Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

EXCEPT & INTERSECT command in SQL Server

3.00/5 (2 votes)
23 Dec 2011CPOL 32.5K  
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)