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 Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>