Chris Cradock (9/1/2010)
I'm not attempting to say HAVING without GROUP BY is invalid, and yes adding MAX(col2) to the selected columns I would then expect the behaviour you describe (and I would also reason the challenge would have been a "doddle" for everyone as it would then be obvious what HAVING was up to).
Its the expectation that HAVING has access to anything that wasn't explicitly worked out at the record selection stage, and thus have access to the MAX(col2) value. SQL server essentially extends the columns selected to resolve the HAVING MAX(col2). So it performs the statement in your response. But that's not in the SQL standard as far as I'm aware.
I hope that clarifies my statement.
Ah, I think I understand. So does this mean that you would have had the same objection if I had included a GROUP BY clause?
WHERE Col2 <> 4
GROUP BY Col1
HAVING MAX(Col2) < 5;
Your misunderstanding is understandable, because we humans are "trained" to read bottom to top (unless you were raised in a culture that writes in a different direction, obviously).
SQL should not be interpreted that way. The logical
expression order of the clauses in a query is:
1. FROM clause (including all joins), to find (and combine) the table(s) worked on;
2. WHERE clause, to throw out individual non-qualifying rows;
3. GROUP BY clause, to combine remaining rows to groups;
4. HAVING clause, to throw out complete non-qualifying groups;
5. SELECT clause, to form the columns in the result set from the columns;
6. ORDER BY, to transform the result from an unordered set to an ordered cursor.
Because this is the logical order of evaluation, expression in the WHERE, GROUP BY, and HAVING clause can not reference the results of expressions in the SELECT clause, but ORDER BY can (so if you have SELECT Col1 + Col2 AS TheSum, you can use TheSum in the ORDER BY clause but nowhere else).
The ANSI standard also says the reverse (that expressions not used in the SELECT clause can not be used in the ORDER BY clause), but SQL Server does allow this. That is a case that you could think of as SQL Server "secretly" adding an extra column to the SELECT list. But again, that is for ORDER BY, not for HAVING.
I hope this clarifies the issue. If not, then please don't hesitate to ask further questions!
Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis