Sequence of fulfillment of UNION, EXCEPT, and INTERSECT operators
When solving exercise #6 (SELECT) of the rating stage we had to answer the question of precedence of UNION, EXCEPT, and INTERSECT operations. Martin Gruber [4] in his “SQL Instant Reference” suggests the following logical sequence of their fulfillment:
If we presume that the logical sequence of fulfillment of operations corresponds to their precedence, than the precedence of UNION and EXCEPT operations is identical and, as a result, they should be fulfilled in accordance with the sequence in which they are recorded, and this sequence is not changed by placement of the brackets. Thereby both of the operations are fulfilled before INTERSECT operation, i.e. they have precedence over it.
Let’s analyze three simple queries, which we will combine in different ways in order to prove the aforementioned claims:
| model | type |
|———|——–|
| 1121 | PC |
| 1750 | Laptop |
| model | type |
|———|——–|
| 1298 | Laptop |
| 1321 | Laptop |
| 1750 | Laptop |
| 1752 | Laptop |
| model | type |
|———|——–|
| 1121 | PC |
| 1232 | PC |
| 1233 | PC |
| 1260 | PC |
| 2111 | PC |
| 2112 | PC |
Let’s prove the first claim. If EXCEPT operation has precedence over UNION operation, then the queries
and
should give us different results. However it is not so and we obtain the same resulting set:
model | type |
---|
1298 | Laptop |
1321 | Laptop |
1750 | Laptop |
1752 | Laptop |
Similarly, if UNION operation has precedence over EXCEPT operation, then the queries
and
should produce different results. But in this case as well we obtain one and the same result:
model | type |
---|
1121 | PC |
1298 | Laptop |
1321 | Laptop |
1750 | Laptop |
1752 | Laptop |
Thus, UNION and EXCEPT operations are equivalent in terms of precedence.
Let’s check precedence of INTERSECT operation as compared to other statements (in test problems one can choose any of them, as both of them have the same sequence).
If INTERSECT is inferior in precedence or equivalent to UNION, then the queries
and
should give identical results. However we obtain different result sets. The first query gives the result
model | type |
---|
1121 | PC |
1750 | Laptop |
——— | ——– |
1121 | PC |
Conclusion. Logical sequence, suggested in the beginning of the article, does not correspond to the order of precedence of the operations, and, to my mind, it should be reversed: