Can I use HAVING instead of WHERE in SQL queries?

I always thought I couldn’t, but MSDN says otherwise.

When GROUP BY is not used, HAVING behaves like a WHERE clause.

I checked and got the error:

Msg 8121:
The column "..." is not valid in the HAVING clause because it is not contained in the aggregate function or in GROUP BY.

So what is this? A mistake in the documentation or a little-known detail?

This may be one of those things that worked in Sybase SQL Server. Now Microsoft has rewritten SQL Server closer to the ANSI standard, but forgot to fix the documentation.

In this case?

+4
source share
1 answer

having group by, .

select avg(price) from tbltemp having avg(price) >= 2

MSDN .

- " avg (price) HAVING a >= 1000", " avg (price) from goods WHERE price >= > 1000"

+6

All Articles