Specifies a predicate that SQL evaluates to generate an
intermediate result table. SQL evaluates the predicate for each
group of the intermediate result table created by a preceding
clause. The groups of that table for which the predicate is true
become another intermediate result table to which SQL applies the
select list for evaluation.
If the clause preceding the HAVING clause is a GROUP BY
clause, then the predicate is evaluated for each group in the
intermediate result table. The HAVING clause affects groups just
as the WHERE clause affects individual rows.
If the HAVING clause is not preceded by a GROUP BY clause, SQL
evaluates the predicate for all the rows in the intermediate
result table as a single group.
SQL restricts which expressions you can specify in the predicate
of a HAVING clause. A column name or expression in a HAVING
predicate must meet one of the following criteria:
o It must also appear in the GROUP BY clause.
o It must be specified within an aggregate function.
o It must be an outer reference (possible only if the HAVING
clause is part of a column select expression).
For instance, the following statement is invalid. It has a
HAVING clause without a GROUP BY clause, which means that any
column names in the HAVING clause must be part of a function
(because there is no outer query, the column names cannot be
outer references).
SQL> SELECT LAST_NAME, FIRST_NAME FROM EMPLOYEES
cont> HAVING FIRST_NAME = 'Bob';
%SQL-F-NOTGROFLD, Column FIRST_NAME cannot be referred to in
the select list or HAVING clause because it is not in the GROUP BY clause