Contents Index Performing set operations on query results with UNION, INTERSECT, and EXCEPT Using EXCEPT and INTERSECT

ASA SQL User's Guide
  Summarizing, Grouping and Sorting Query Results
    Performing set operations on query results with UNION, INTERSECT, and EXCEPT

Combining sets with the UNION operation


The UNION operator combines the results of two or more queries into a single result set.

By default, the UNION operator removes duplicate rows from the result set. If you use the ALL option, duplicates are not removed. The columns in the result set have the same names as the columns in the first table referenced. Any number of union operators may be used. For example:

x UNION y UNION z

By default, a statement containing multiple UNION operators is evaluated from left to right. Parentheses may be used to specify the order of evaluation.

For example, the following two expressions are not equivalent, due to the way that duplicate rows are removed from result sets:

x UNION ALL (y UNION z)
(x UNION ALL y) UNION z

In the first expression, duplicates are eliminated in the UNION between y and z. In the UNION between that set and x, duplicates are not eliminated. In the second expression, duplicates are included in the union between x and y, but are then eliminated in the subsequent union with z.


Contents Index Performing set operations on query results with UNION, INTERSECT, and EXCEPT Using EXCEPT and INTERSECT