Collection Contents Index The ORDER BY clause: sorting query results Standards and compatibility pdf/chap5.pdf

User's Guide
   PART 1. Working with Databases
     CHAPTER 5. Summarizing, Grouping, and Sorting Query Results       

The UNION operation: combining queries


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:

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; ALL does not affect the result of this statement.

Guidelines for UNION queries 

The following are guidelines to observe when you use union statements:


Collection Contents Index The ORDER BY clause: sorting query results Standards and compatibility pdf/chap5.pdf