The HAVING Clause

A HAVING clause is similar to a WHERE clause, but it differs in one major respect: SQL Server applies it after it summarizes the data rather than beforehand. In other words, the WHERE clause is used to determine which rows are grouped. The HAVING clause determines which groups are included in the output. A HAVING clause looks like this:

HAVING expression1 [{AND|OR} expression2[...]]
					

In the following example, SQL Server applies the criteria > 1000 after it applies the aggregate function SUM to the grouping. Therefore, SQL Server includes only country/city combinations with total freight greater than 1000 in the output (see Figure 6.17).

SELECT Customers.Country, Customers.City,
   Sum(Orders.Freight) AS SumOfFreight
   FROM Customers ...

Get Alison Balter's Mastering Access 2002 Enterprise Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.