SQL UNION Keyword
The UNION command combines the result sets of two or more SELECT statements into a single result set, returning only distinct values. The columns in each SELECT must have the same number of columns, in the same order, and with compatible data types.
SQL UNION Syntax
SQL
x
1
SELECT column1, column2, ...
2
FROM table1
3
UNION
4
SELECT column1, column2, ...
5
FROM table2
6
ORDER BY column_name;
SQL UNION Example
This query combines distinct gender
values from Patients
and distinct specialty
values from Doctors
as a single list.
SQL
1
1
SELECT gender AS info
2
FROM Patients
3
UNION
4
SELECT specialty
5
FROM Doctors
6
ORDER BY info;
