Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234

HAVING without GROUP BY Expand / Collapse
Author
Message
Posted Wednesday, September 1, 2010 7:02 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:16 PM
Points: 6,141, Visits: 8,404
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?
SELECT COUNT(*)
FROM QotD
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
Post #978715
Posted Wednesday, September 1, 2010 8:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:46 AM
Points: 13,646, Visits: 11,524
Nice question. But I believe I've seen a similar one here in the QotD (but I may be mistaken).



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #978796
Posted Wednesday, September 1, 2010 8:27 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:16 PM
Points: 6,141, Visits: 8,404
da-zero (9/1/2010)
Nice question. But I believe I've seen a similar one here in the QotD (but I may be mistaken).

Correct. That was April 14, and it was similar but not identical.
http://www.sqlservercentral.com/questions/T-SQL/70030/



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #978822
Posted Wednesday, September 1, 2010 8:29 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:46 AM
Points: 13,646, Visits: 11,524
Hugo Kornelis (9/1/2010)
da-zero (9/1/2010)
Nice question. But I believe I've seen a similar one here in the QotD (but I may be mistaken).

Correct. That was April 14, and it was similar but not identical.
http://www.sqlservercentral.com/questions/T-SQL/70030/


I knew it
I was just too lazy to look it up.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #978825
Posted Wednesday, September 1, 2010 5:15 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, October 24, 2014 4:29 PM
Points: 872, Visits: 954
Great question! Thanks.
Post #979218
Posted Tuesday, September 7, 2010 4:03 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 11, 2013 9:52 PM
Points: 125, Visits: 29
Excellent question. I was not aware of this and I gave a wrong answer
Post #981428
Posted Tuesday, September 7, 2010 11:24 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:18 PM
Points: 7,930, Visits: 9,654
This was real fun. So was the subsequent discussion. I really ought to get back to looking at QOTD regularly as there seem to be some real gems around now instead of the dire stuff of 6 months ago.

I got the right answer by applying a logically wrong method which I've used before (pretend that any aggregates in the having clause but not in the select clause are added to the select clause, that the having clause then operates on the result of the combined select and group by clauses, and that after executing the having clause any spurious additions to the select clause are discarded so that the result has only the required columns). Although this is far from what logically happens it usually delivers the same results (if I've read the standard correcly, that "usually" is actually "always", so that doing it that way - if it were efficient - would be a valid option for an optimizer).


Tom
Post #981763
Posted Tuesday, September 7, 2010 12:52 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 7:34 AM
Points: 450, Visits: 282
It is real joy to answer such questions. I was able to look at so usual command from another point, turn on common logic and get it right. But I was not sure I was right. Thanks!


Post #981826
Posted Thursday, September 16, 2010 5:41 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 12:15 AM
Points: 9,928, Visits: 11,207
Excellent question Hugo - though I too chose the wrong answer.



Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #987136
Posted Monday, March 19, 2012 10:25 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Saturday, November 29, 2014 2:17 AM
Points: 483, Visits: 245
Great question and analogy on the HAVING... GROUP BY.
Post #1269085
« Prev Topic | Next Topic »

Add to briefcase «««1234

Permissions Expand / Collapse