The SQL UNION operator is used to combine the results of two or more SELECT queries. Each SELECT statement within the UNION must
have the same number of columns and similar data types.
SA Employees Table
employeeID | employeeName |
1000 | John Smith |
1001 | Fred White |
1002 | Jane Scott |
1003 | Samuel Williams |
NA Employees Table
employeeID | employeeName |
2000 | Maria Garcia |
2001 | Jane Reed |
2002 | Jack Williams |
2003 | Fran Orman |
In this example, we have two different tables that contain employee data. We can use the SQL Union Operator to combine the results from
two select queries.
Syntax
SELECT tableName1.columnName1, tableName1.columnName2, etc...
FROM tableName1
UNION
SELECT tableName2.columnName1, tableName2.columnName2, etc...
FROM tableName2
|
Example
List the employee data for SA and NA employees.
SELECT Employees_SA.employeeID, Employees_SA.employeeName
FROM Employees_SA
UNION
SELECT Employees_NA.employeeID, Employees_NA.employeeName
FROM Employees_NA
|
Results
employeeID | employeeName |
1000 | John Smith |
1001 | Fred White |
1002 | Jane Scott |
1003 | Samuel Williams |
2000 | Maria Garcia |
2001 | Jane Reed |
2002 | Jack Williams |
2003 | Fran Orman |
The UNION operator combines the results from both queries.
Did you find the page informational and useful? Share it using one of your favorite social sites.
Recommended Books & Training Resources