SQL Server Notes #3 – Except and Intersect
I’m sure you’ve all heard about the Union operator, it joins result sets together into a single record set. I haven’t used UNION very often but it’s definitely one of those operations that you are happy for when you need it. The thought of having to write a bunch of temporary table, select/insert queries to get the same data is not an appealing one.
Along with UNION there are two other similar operations that perform logic upon those result sets instead of just pushing them together and returning them. These operations are EXCEPT and INTERSECT. Some of you will probably be laughing at me when I tell you that I wasn’t aware of the EXCEPT and INTERSECT operators. Well you don’t have to laugh any more because now I know! Simply put these operators do much the same thing as UNION except that they perform comparisons between the results of two queries and produce a result set based on the existence of the records in both tables. Good stuff.