Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
UNION.docx59 KB UNION - Solution.docx
59.5 KB
Quick reference
UNION
The Union operator combines the results of two or more Select statements.
When to use
Use it when you want to combine two or more Select statements or statements from different tables.
Instructions
SELECT [Email Address]
FROM [Customer].[dbo].[Current_Customers]
UNION
SELECT [Country]
FROM [Customer].[dbo].[Current_Customers]
This will give us one column with all the customer email addresses and countries in ascending alphabetical order.
UNION is particularly useful for combining statements from multiple tables into one column, for example if you wanted to combine email addresses from multiple tables into one list:
SELECT [Email Address]
FROM [Customer].[dbo].[Current_Customers]
UNION
SELECT [Email Address]
FROM [Customer].[dbo].[Past_Customers]
This will give us one column with all the customer email addresses from past customers and current customers in ascending alphabetical order.
Hints & tips
- Every Select Statement has to have the same number of columns to use UNION
- The Columns have to have the same data type
- The Columns of each Select Statement must be in the same order
Lesson notes are only available for subscribers.