Simple-Minded Syntax

SQL Server 2008's EXCEPT and INTERSECT can produce filtered results that require almost no brain power.

SQL Server 2008 shipped with several enhancements to the T-SQL language. Last time, we looked at the new MERGE syntax. This time, let's check out the new EXCEPT and INTERSECT syntax. Both of these operators allow you to compare results of two queries. Using EXCEPT, you can find all the rows that exist in one query but not the other. Using INTERSECT, you can find all the rows that are the same in both tables. It'll make more sense with an example, so let's take a look.

I took a copy of the HumanResources.Employee table from AdventureWorks and made two copies called Employee1 and Employee2. Then to give us different results, I ran the following delete statement:

DELETE FROM Employee1 WHERE EmployeeID < 10
DELETE FROM Employee2 WHERE EmployeeID > 300

This gives us two tables that have matching records as well as unique records that the other table doesn't have. Now we can explore the statements. First let's run an EXCEPT comparison:

SELECT * FROM Employee1
SELECT * FROM Employee2

This will return the rows in Employee1 that are not also found in Employee2; in this case, it's all the records with an EmployeeID greater than 300. Reversing the location of the SELECT statements would return all the rows in Employee2 that were not also found in Employee1 (that is, all rows with an EmployeeID less than 10).

Next let's use INTERSECT . The following statement will return all the records that are contained in both queries -- in our case, all records with an EmployeeID between 10 and 300:

SELECT * FROM Employee2
SELECT * FROM Employee1

These are great little additions to T-SQL that allow you to do some quick comparision work on your result sets. With a few twists, you could even write a query that returns all rows from both tables with no match in the other table.

Next time you find yourself with data to compare, give EXCEPT and INTERSECT a shot.

About the Author

Eric Johnson, SQL Server MVP, is the owner of Consortio Services in Colorado Springs providing IT systems management and technology consulting. He is also the President of the Colorado Springs SQL Server User Group. He can be contacted at


  • Microsoft Nabs IoT Platform Provider Express Logic

    As part of its plan to invest $5 billion in IoT technologies, Microsoft this week acquired Express Logic, which provides real-time operating systems for industrial embedded and IoT devices.

  • Dealing with Broken Dependencies in SCVMM

    Brien shows you how to resolve some broken, template-related dependencies in Microsoft's System Center Virtual Machine Manager.

  • AzCopy Preview Adds AWS S3 Data Transfer Improvements

    Microsoft announced this week that it has improved the preview version of its AzCopy tool to better handle Amazon Web Services (AWS) S3 data.

  • Microsoft Adding Google G Suite Migration in Exchange Admin Center

    Microsoft's Exchange Admin Center will be getting the ability to move Google G Suite calendar, contacts and e-mail data over to the Office 365 service "in the coming weeks."

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.