15.00 - Precedence of Set Operators - Teradata Database

Teradata Database SQL Functions, Operators, Expressions, and Predicates

Product
Teradata Database
Release Number
15.00
Content Type
Programming Reference
Publication ID
B035-1145-015K
Language
English (United States)
Last Update
2018-09-24

Precedence of Set Operators

The precedence for processing set operators is as follows:

1 INTERSECT

2 UNION and MINUS/EXCEPT

The set operators evaluate from left to right if no parentheses explicitly specify another order.

Example  

For example, consider the following query.

   SELECT statement_1 
   UNION
   SELECT statement_2 
   EXCEPT 
   SELECT statement_3 
   INTERSECT 
   SELECT statement_4;

The operations are performed in the following order:

1 Intersect the results of statement_3 and statement_4.

2 Union the results of statement_1 and statement_2.

3 Subtract the intersected rows from the union.

Using Parentheses to Customize Precedence

To override precedence, use parentheses. Operations in parentheses are performed first.

For example, consider the following form:

   ( ( SELECT statement_1 
       UNION 
       SELECT statement_2 )
   EXCEPT
     ( SELECT statement_3 
       UNION 
       SELECT statement_4 )
   )
   EXCEPT 
   SELECT statement_5 
   INTERSECT 
   SELECT statement_6;

The following list explains the precedence of operators for this example.

1 UNION SELECT statement_1 and SELECT statement_2.

2 UNION SELECT statement_3 and SELECT statement_4.

3 Subtract the result of the second UNION from the result of the first UNION.

4 INTERSECT SELECT statement_5 and SELECT statement_6.

5 Subtract the INTERSECT result from the remainder of the UNION operations.