SQL UNION

The SQL UNION query is used to merge two or more SQL SELECT query results. Each query statement should have same column structure: same number of columns, same or compatible data types and in same order.

Note: The result of SQL UNION query is distinct.

SQL UNION Query Syntax

SELECT Column(s) FROM Table1
UNION
SELECT Column(s) FROM Table2

SQL UNION Query Example

Table: FinanceEmployees

EmployeeId FirstName LastName
203 Kilooa Joooo
204 Hioiaaa Hifa
205 Yuiofa Iioooa
206 Miofa Poqqq

Table: DevelopmentEmployees

EmployeeId FirstName LastName
206 Miofa Poqqq
207 Qifaww Siafoa
208 Ziooo Rrroo
209 UUifad Bbiiw

Select all the different employees in FinanceEmployees and DevelopmentEmployees:

SELECT EmployeeId, FirstName, LastName FROM FinanceEmployees 
UNION
SELECT EmployeeId, FirstName, LastName FROM DevelopmentEmployees

The result will look like:

EmployeeId FirstName LastName
203 Kilooa Joooo
204 Hioiaaa Hifa
205 Yuiofa Iioooa
206 Miofa Poqqq
207 Qifaww Siafoa
208 Ziooo Rrroo
209 UUifad Bbiiw

Leave a Comment