SQL UNION

The SQL UNION clause merges the results of two or more SELECT SQL queries into one result set. When using SQL UNION, all the SQL expressions participating in the UNION must have the same structure (they have to have the same number of columns and same or compatible data types). You have to keep the column order of all unionized SQL SELECT expressions uniform, otherwise you'll get an error.

Here is an example of SQL UNION statement:

SELECT City, AverageTemperature, Date FROM Weather UNION SELECT City, AverageTemperature, Date FROM WeatherArchive

The result of the SQL UNION statement above will merge all records from the Weather and the WeatherArchive tables and the column names in the result will remain the same as the column names in the first SELECT expression in the UNION statement. This SQL UNION expression will also remove all duplicates (if any).

Something important to remember when using SQL UNION is that the duplicated rows are removed from the result set. If you want all rows, including duplicate ones to be returned, simply put the ALL keyword after the UNION clause:

SELECT City, AverageTemperature, Date FROM Weather UNION ALL SELECT City, AverageTemperature, Date FROM WeatherArchive