Evaluation Order of WHERE, GROUP BY, and QUALIFY Clauses - Teradata Database - Teradata Vantage NewSQL Engine

SQL Data Manipulation Language

Product
Teradata Database
Teradata Vantage NewSQL Engine
Release Number
16.20
Published
March 2019
Language
English (United States)
Last Update
2019-05-03
dita:mapPath
fbo1512081269404.ditamap
dita:ditavalPath
TD_DBS_16_20_Update1.ditaval
dita:id
B035-1146
lifecycle
previous
Product Category
Teradata Vantage™
When the WHERE, GROUP BY, and QUALIFY clauses are used together in a SELECT statement, the order of evaluation is as follows:
  1. WHERE clause
  2. GROUP BY clause
  3. QUALIFY clause
The detailed steps are as follows:
  1. Teradata Database evaluates the WHERE clause conditions on the FROM clause tables.
  2. The system groups the resulting rows using the GROUP BY columns.
  3. Teradata Database evaluates the ordered analytical functions on the grouped table.
  4. The system applies the QUALIFY clause to the resulting set.

Teradata Database-specific functions such as CSUM and MAVG that are invoked in both the select list and in the search condition of the QUALIFY clause are computed on the grouped rows without eliminating any rows and then the system applies the search condition of the QUALIFY clause.

For window functions, such as SUM and AVG, the GROUP BY collapses all rows with the same value for the group-by columns into a single row.