A conditional clause in the SELECT statement that filters results of a previously computed ordered analytical function according to user‑specified search conditions.
where:
Syntax element … |
Specifies … |
QUALIFY |
an introduction to a conditional clause that, similar to HAVING, further filters rows from a WHERE clause. The major difference between QUALIFY and HAVING is that with QUALIFY the filtering is based on the result of performing various ordered analytical functions on the data. |
search_condition |
one or more conditional expressions that must be satisfied by the result rows. You can specify ordered analytical functions and scalar subqueries as search conditions with QUALIFY. You can use aggregate operators and DEFAULT functions within a QUALIFY search condition. The QUALIFY condition cannot reference LOB columns. If you specify the value for a row‑level security constraint in a search condition, that value must be expressed in its encoded form. |
The QUALIFY clause is a Teradata extension to the ANSI standard.
The rules and restrictions are:
The following query fails because of the OR condition connected with the subquery.
SELECT RANK (column_1)
FROM table_1
QUALIFY column_1 IN (SELECT table_2.column_1
FROM table_2)
OR table_1.column.1 = 10000;
A condition other than IS NULL or IS NOT NULL with a DEFAULT function compared with a null evaluates to unknown.
IF a DEFAULT function is used with … |
THEN the comparison is … |
IS NULL |
|
IS NOT NULL |
|
See SQL Functions, Operators, Expressions, and Predicates for more information about the DEFAULT function.
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.
Tables referenced in a QUALIFY clause must be specified in one of the following:
Otherwise, the system returns an error to the requestor. QUALIFY is similar to HAVING in that rows are eliminated based on the value of a function computation. With QUALIFY, rows are eliminated based on the computation of the ordered analytical functions.