Using Unions

Objective

After completing this lesson, you will be able to retrieve data from multiple sets.

Unions Overview

The SELECT statement is more flexible than has been shown so far. It can be used to combine data from one or more tables. This may involve combining rows or columns in novel ways.

You can combine the results of several partial queries to form the union of the results, or to determine their intersection or difference:

  • UNION ALL and UNION merge the results of partial queries to form their union.

  • INTERSECT is used to determine the elements contained in all partial query results.

  • EXCEPT is used to determine the elements contained in one partial result, but not the second partial result.

UNION and UNION ALL

UNION [ALL]

You can combine the result sets of multiple queries using UNION [ALL].

  • The individual result sets must have the same number of columns.
  • The corresponding result columns must have compatible data types.
  • The column names of the resulting output table are based on the first SELECT statement.
Code Snippet
123456789
SELECT PNr, Name FROM Official WHERE Salary = 'A09' UNION ALL SELECT OwnerID, Name FROM Owner WHERE Birthday >= '1977-05-21';
PNRNAME
P01Mr A
P03Ms C
P06Mr F
H08Mr X
H09Ms Y
H10Mr Z

UNION ALL Can Lead to Duplicates

If the results of multiple partial queries overlap, the UNION ALL operator includes duplicates.

Code Snippet
123456789
SELECT PlateNumber, Brand, Color FROM Car WHERE Brand = 'Mercedes' UNION ALL SELECT PlateNumber, Brand, Color FROM Car WHERE Color = 'white';
PLATENUMBERBRANDCOLOR
HD-AL 1002Mercedeswhite
HD-MM 3206Mercedesblack
HD-MM 1977Mercedeswhite
HD-AL 1002Mercedeswhite
HD-MM 1977Mercedeswhite

The duplicates are marked in bold.

Avoiding Duplicates Using UNION

You use UNION instead of UNION [ALL] to eliminate duplicates.

Code Snippet
123456789
SELECT PlateNumber, Brand, Color FROM Car WHERE Brand = 'Mercedes' UNION SELECT PlateNumber, Brand, Color FROM Car WHERE Color = 'white';
PLATENUMBERBRANDCOLOR
HD-AL 1002Mercedeswhite
HD-MM 3206Mercedesblack
HD-MM 1977Mercedeswhite

UNION [ALL] Can Handle More Than Two SELECTs

You can use UNION [ALL] to combine result tables of multiple queries.

Code Snippet
123456789101112131415
SELECT PlateNumber, Brand, Color FROM Car WHERE Brand = 'BMW' UNION SELECT PlateNumber, Brand, Color FROM Car WHERE Color = 'yellow' UNION SELECT PlateNumber, Brand, Color FROM Car WHERE Color = 'orange';
PLATENUMBERBRANDCOLOR
HD-JA 1972BMWblue
HD-MT 507BMWblack
HD-MM 208BMWgreen
HD-VW 1999Audiyellow
HD-Y 333Audiorange

INTERSECT and EXCEPT

INTERSECT [DISTINCT]

INTERSECT returns records that exist in all query results.

Code Snippet
123456789
SELECT PlateNumber, Brand, Color FROM Car WHERE Brand = 'Mercedes’ INTERSECT SELECT PlateNumber, Brand, Color FROM Car WHERE Color = 'white';
PLATENUMBERBRANDCOLOR
HD-AL 1002Mercedeswhite
HD-MM 1977Mercedeswhite

EXCEPT [DISTINCT]

EXCEPT returns results from the first query that are NOT available in subsequent queries.

Code Snippet
12345678910
SELECT PlateNumber, Brand, Color FROM Car WHERE Brand = 'Mercedes’ EXCEPT SELECT PlateNumber, Brand, Color FROM Car WHERE Color = 'white';
PLATENUMBERBRANDCOLOR
HD-MM 3206Mercedesblack

Relationships Between the UNION [ALL], INTERSECT, and EXCEPT Operators

Venn diagrams showing the relationships between the UNION [ALL], INTERSECT, and EXCEPT operators.