Fine!Are the folowing two SELECT statements equvalent (i.e. -- see my original question, -- "return exactly the same row set" no matter "what data is")?SELECT xx FROM yy WHERE xx >=5 GROUP BY xxSELECT xx FROM yy GROUP BY xx HAVING xx >=5

Igor Makedon (1/28/2013)Fine!Are the folowing two SELECT statements equvalent (i.e. -- see my original question, -- "return exactly the same row set" no matter "what data is")?SELECT xx FROM yy WHERE xx >=5 GROUP BY xxSELECT xx FROM yy GROUP BY xx HAVING xx >=5

NO, they are not equivalent, atlhough they may return the same result set.

Since xx is a non-aggregate column, "WHERE xx ..." is correct.

In some SQLs, SQL will not check xx with HAVING until after the GROUPing is done. SQL Server now effectively rewrites the second query to match the first. But it's wrong to rely on that. I would suspect the second query is not even guaranteed to be accepted by SQL itself as a valid query -- might depend on the SQL engine and/or version.

SQL DBA,SQL Server MVP('07, '08, '09)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!