Try the Introductory SQL Course for Free!

The UNION Operator in SQL- Tutorial

/ / Access 2013, Latest, Office 2013, SQL

The UNION Operator in SQL: Video Lesson

            This video lesson, titled “The UNION Operator,” shows you how to use the UNION operator in SQL. It uses an example that shows how to apply the UNION operator within an Access database. Note that this video is from our complete introductory SQL training, titled “Mastering Introductory SQL Made Easy v.1.0.”

The UNION Operator in SQL: Overview

            The UNION operator in SQL allows you to combine the result sets of two or more individual SELECT statements into a single result set. Note that the UNION operator in SQL is not a JOIN, but rather a way to combine the data within two result sets into the same fields in single result set. Because of this, the UNION operator in SQL can only combine two SELECT statements with the same number of fields. These fields must also share compatible data types within the two separate SELECT statements. If combining two SELECT statements that contain multiple fields, the fields must also be in the same order from left to right.

            By default, the UNION operator in SQL will only return unique values in the fields that it is combining. If you want the UNION operator in SQL to return ALL records from both SELECT statements, then you must use the UNION ALL clause, instead. Also, note that if combining fields with different field name values, the field name values of the first SELECT statement will be the ones that are used in the combined result set.

            The core SQL of the UNION operator in SQL is shown below. The “select_statement” parameter is simply the SELECT statement that you want to combine with another SELECT statement.

select_statement

UNION

select_statement1

or

select_statement

UNION ALL

select_statement1

The UNION Operator in SQL- Tutorial: A picture from the video lesson for "The UNION Operator" within the Introductory SQL training, titled "Mastering Introductory SQL Made Easy v.1.0."

The UNION Operator in SQL- Tutorial: A picture from the video lesson for “The UNION Operator” within the Introductory SQL training, titled “Mastering Introductory SQL Made Easy v.1.0.”

The UNION Operator in SQL: Additional References

            The following web page hyperlinks list the syntax of the UNION operator in SQL when used within MySQL 5.7, SQL Server 2012, and Access 2013.

MySQL 5.7:

https://dev.mysql.com/doc/refman/5.7/en/union.html

SQL Server 2012:

https://technet.microsoft.com/en-us/library/ms180026.aspx

Access 2013:

https://msdn.microsoft.com/en-us/library/office/ff821131.aspx

TOP