HAVING without GROUP BY

  • 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/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Nice question. But I believe I've seen a similar one here in the QotD (but I may be mistaken).

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • 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://qa.sqlservercentral.com/questions/T-SQL/70030/[/url]


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • 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://qa.sqlservercentral.com/questions/T-SQL/70030/[/url]

    I knew it 🙂

    I was just too lazy to look it up. 🙂

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Great question! Thanks.

  • Excellent question. I was not aware of this and I gave a wrong answer 😀

  • 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

  • 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!

  • Excellent question Hugo - though I too chose the wrong answer.

  • Great question and analogy on the HAVING... GROUP BY.

Viewing 10 posts - 31 through 39 (of 39 total)

You must be logged in to reply to this topic. Login to reply