36
Microsoft SQL Server 2012 T-SQL Fundamentals
The
HAVING
Clause
With the HAVING clause, you can specify a predicate to filter groups as opposed to filtering individual
rows, which happens in the WHERE phase. Only groups for which the logical expression in the HAVING
clause evaluates to TRUE are returned by the HAVING phase to the next logical query processing
phase. Groups for which the logical expression evaluates to FALSE or UNKNOWN are filtered out.
Because the HAVING clause is processed after the rows have been grouped, you can refer to ag-
gregate functions in the logical expression. For example, in the query from Listing 2-1, the HAVING
clause has the logical expression COUNT(*) > 1, meaning that the HAVING phase filters only groups
(employee and order year) with more than one row. The following fragment of the Listing 2-1 query
shows the steps that have been processed so far.
FROM Sales.Orders
WHERE custid = 71
GROUP BY empid, YEAR(orderdate)
Do'stlaringiz bilan baham: |