The SQL UNION clause
The SQL UNION clause
is used to combine the results of two or more SELECT statements without
returning any duplicate rows.
To use UNION, each SELECT must have the same number of columns
selected, the same number of column expressions, the same data type, and have
them in the same order, but they do not have to be the same length.
Syntax:
The basic syntax of UNION is
as follows:
SELECT column1 [, column2 ]
FROM table1 [,
table2 ]
[WHERE condition]
UNION
SELECT column1 [,
column2 ]
FROM table1 [,
table2 ]
[WHERE condition]
The UNION ALL Clause:
The UNION ALL operator is used to combine the results of two
SELECT statements including duplicate rows.
There are two other clauses (i.e., operators), which are very
similar to UNION clause:
·
SQL INTERSECT Clause:
is used to combine two SELECT statements, but returns rows only from the first
SELECT statement that are identical to a row in the second SELECT statement.
·
SQL EXCEPT Clause : combines two SELECT statements and
returns rows from the first SELECT statement that are not returned by the
second SELECT statement.