The HAVING keyword specifies a search condition for a particular group or aggregate. HAVING is usually used with the GROUP BY clause to limit the rows returned by the query. When GROUP BY is not used, HAVING behaves like a WHERE clause, which is why I gave it its own section. The HAVING syntax is as follows:
[HAVING < search_condition >] Arguments < search_condition >
The search condition here specifies the search condition for the group or the aggregate to meet. When HAVING is used with GROUP BY ALL, the HAVING clause overrides ALL.
Note | Columns of type varchar(max), nvarchar(max), or varbinary(max) cannot be used in aHAVING clause. Also, the HAVING clause in the SELECT statement does not affect the waythe CUBE operator groups the result set and returns summary aggregate rows. |
Getting back to our simple example, if we now add a HAVING clause as follows:
SELECT Item, Quantity, Category FROM Items HAVING Category LIKE 'Animals'
the following result set is returned:
Item | Quantity | Category |
---|---|---|
Apes | 3434 | Animals |
Monkeys | 342 | Animals |