The Oracle UNION clause combines the results of two or more SQL queries into a single result set.
The queries must result in the same number of columns and compatible data types.
SELECT column1, column2,....
FROM tablename1,...
WHERE condition1
UNION [ALL]
SELECT column1, column2,...
FROM tablename2,...
WHERE condition2
It removes duplicate rows between the various SELECT statements (unless UNION ALL is used).
Example 1:
SELECT countrycode, countryname FROM country
UNION
SELECT countrycode, countryname FROM country_bak;
UNION does not guarantee the order of rows.
In situations where a specific order is desired, ORDER BY must be used.
Example 2:
SELECT countrycode, countryname FROM country
UNION
SELECT countrycode, countryname FROM country_bak
ORDER BY 2;
UNION ALL gives different results, because it will not remove duplicates.
Example 3:
SELECT countrycode, countryname FROM country
UNION ALL
SELECT countrycode, countryname FROM country_bak;
Note that UNION ALL may be much faster than plain UNION.