Полезная информация

next up previous contents index
Next: Joining Tables Up: SQL Aggregates Previous: Using GROUP BY

Using HAVING

There is one more aggregate capability that is often overlooked. It is the HAVING clause. HAVING allows you to perform conditional tests on aggregate values. It is almost always used with GROUP BY. With HAVING, you can include/exclude groups based on the aggregate value for that group. For example, suppose you want to know all the states where there is more than one friend. Looking at the first query in figure [*], you can see exactly which states have more than one friend. HAVING allows you to programmatically test on the count column, as shown in figure [*].

  
Figure: HAVING usage
\begin{figure}\begin{list}{}{
\setlength{\rightmargin}{\leftmargin}
\raggedrigh...
...ar MA~~~\vert~~~~2
\par NJ~~~\vert~~~~2
\par (2~rows)\end{list}\par
\end{figure}

Aggregates can't be used in a WHERE clause. They are valid only inside HAVING.


Bruce Momjian
1999-11-21