You are on page 1of 10

Click to edit Master

subtitle style

04 | Using Set Operators

Graeme Malcolm | Senior Content Developer, Microsoft


Geoff Allix | Principal Technologist, Content Master
Module Overview

• What are UNION Queries?


• What are INTERSECT Queries?
• What are EXCEPT Queries?
What are UNION Queries?

• UNION returns a result set of distinct rows combined from all


statements
• UNION removes duplicates during query processing (affects
performance)
• UNION ALL retains duplicates during query processing

-- only distinct rows from both queries are returned


SELECT countryregion, city FROM HR.Employees
UNION
SELECT countryregion, city FROM Sales.Customers;
UNION Guidelines

• Column aliases
– Must be expressed in first query
• Number of columns
– Must be the same
• Data types
– Must be compatible for implicit conversion (or converted explicitly)
DEMO
Creating UNION Queries
What are INTERSECT Queries?

• INTERSECT returns only distinct rows that appear in both result


sets

-- only rows that exist in both queries will be returned


SELECT countryregion, city FROM HR.Employees
INTERSECT
SELECT countryregion, city FROM Sales.Customers;
What are EXCEPT Queries?

• EXCEPT returns only distinct rows that


appear in the first set but not the second
– Order in which sets are specified matters

-- only rows from Employees will be returned


SELECT countryregion, city FROM HR.Employees
EXCEPT
SELECT countryregion, city FROM Sales.Customers;
DEMO
Demo: Creating INTERSECT and EXCEPT Queries
Using Set Operators

• What are UNION Queries?


• What are INTERSECT Queries?
• What are EXCEPT Queries?

• Lab: Using Set Operators


©2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the
U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft
must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after
the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

You might also like