SQL UNION combine the results of two queries
together. In this respect, UNION is similar to JOIN in that they are
both used to related information from multiple tables. One restriction of UNION
is that all corresponding columns need to be of the same data type
and UNION only select distinct values.
Syntax:
[SQL Statement 1]
UNION
[SQL Statement 2]
Table Store_Information
Store_name
Sales
Date
Los Angeles
$1500
Jan-05-2005
San Diego
$250
Jan-07-2005
Los Angeles
$300
Jan-08-2005
Boston
$700
Jan-08-2005
Table Internet_Sales
Date
Sales
Jan-07-2005
$250
Jan-10-2005
$535
Jan-11-2005
$320
Jan-12-2005
$750
If we want to find out all the dates where there is a sales transaction.We use the following SQL statement:
SELECT Date FROM Store_Information
UNION
SELECT Date FROM Internet_Sales
The SQL UNION ALL command is also to combine the results of two queries together. The difference between UNION ALL and UNION is that, UNION ALL selects all values while UNION only selects distinct values.
Syntax :
SELECT Date FROM Store_Information
UNION ALL
SELECT Date FROM Internet_Sales