SQL Union Select Join Command

The SQL UNION command is used to gather the entries from two tables which meet the given criteria.

The SQL Union command is used to select entries from two tables It requires that the items in each of the select are in the same order and that each comparable item in the same position is of the same data type and length.

Consider a typical example of two sets of employees located in two different countries, those in England and France:

England – table name =  Users_GB

Ref. (UserId)First Name (FName)Last Name (LName)
006PeterSmith
008StevenJones
023RuthMortimer
034KurtFulmore
184LindaLoveridge

France – table name = Users_FR

Ref. (UserId)First Name (FName)Last Name (LName)
007LindaLoveridge
026LouisaRevel
027MarkMitchell
101AlanBrown
128JamesWoodrow
SELECT FName,LName FROM Users_GB
UNION
SELECT FName,LName FROM Users_FR

As given above the command will select distinct entries, where entries are common to both tables only one entry will be returned.

UNION combined table

First Name (FName)Last Name (LName)
PeterSmith
StevenJones
RuthMortimer
KurtFulmore
LindaLoveridge
LouisaRevel
MarkMitchell
AlanBrown
JamesWoodrow

To return all entries from both tables the Union all command is used:

SELECT FName,LName FROM Users_GB
UNION ALL
SELECT FName,LName FROM Users_FR

UNION ALL combined table

First Name (FName)Last Name (LName)
PeterSmith
StevenJones
RuthMortimer
KurtFulmore
LindaLoveridge
LindaLoveridge
LouisaRevel
MarkMitchell
AlanBrown
JamesWoodrow