UNION
A UNION operator combines the results of two or more selects, giving all the rows returned by each select as one single result table.
UNION syntax:
SELECT statement_1
UNION [ALL | DISTINCT]
SELECT statement_2
UNION [ALL | DISTINCT]
SELECT statement_3 …
UNION …….. establishes which result sets will be combined into one result set.
Duplicate rows and nulls are excluded by default.
ALL …………. means that duplicate rows are not to be excluded.
DISTINCT … means duplicate rows and nulls are to be excluded.
If neither ALL or DISTINCT are specified, then DISTINCT is the default.
> The multiple selects must have the same number of columns (column names, functions, or arithmetic expressions).
> The order of columns must match in data type or be convertible to the same data type.
> If the name in a column is the same in all selects, then this name is used in the result. If this is not true, then most RDBMS systems take the name from the first select. If you want to create a different column name, use an AS clause in the first select for a column alias.
> ORDER BY can only be used in the last select, but it is applied to the entire result table.
> GROUP BY and HAVING can be used in each select statement, but do not apply to the final result table.
> Access, DB2, Oracle, PostgreSQL, and SQL Server support UNION and UNION ALL.
> MySQL does not support UNION or UNION ALL.