HAVING

  • Paul White NZ (6/21/2010)


    Rick Lang (6/21/2010)


    Perhaps a different version of the question where the answer would be different with the Having clause vs just the where clause might help to clear up any confusion (mine anyways)?

    In addition to Hugo's example, this is a favourite of mine:

    SELECT 'One row'

    WHERE 1 = 0

    HAVING 0 = 0;

    Wow!

    You should have saved that for QotD, Paul!


    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/

  • John McC (6/22/2010)


    That's just plain wrong. 😉

    It still gives me nightmares from time to time :blink:

    I used to think I understood the basics of SQL.

  • I've seen that one before somewhere.

    It becomes slightly more obvious if you add a 'count(*)' to the select list

  • Hugo Kornelis (6/22/2010)


    Wow!

    You should have saved that for QotD, Paul!

    Scary isn't it!

    I blame a fellow MVP of yours: Rob Farley for all this - he reminded me of it in a T-SQL Tuesday blog a little while ago. I didn't have the link to hand before, but I've found it now, for anyone else that's interested:

    http://msmvps.com/blogs/robfarley/archive/2010/01/12/t-sql-tuesday-having-puzzle-answer.aspx

    I don't recall where I first came across it - it just seems to spring up every few years to remind me that SQL doesn't always go hand in hand with common sense...:-)

  • Toreador (6/22/2010)


    It becomes slightly more obvious if you add a 'count(*)' to the select list

    Yep there's lots of variations:

    SELECT 'One row', SUM(CASE WHEN 1 = 0 THEN 0 ELSE 1 END)

    WHERE 1 = 0;

  • Hi,

    Could not get the having clause ...

    can anyonce please explain?

  • me_always (8/9/2010)


    Hi,

    Could not get the having clause ...

    can anyonce please explain?

    Sure - but such a broad question is too hard to answer (other than with a reference to Books Online).

    Have you read the explanation that is given after replying to the question? And have you read the discussion here?

    If, after reading all that, you still have a question, I'll be happy to answer - as soon as you tell me what exactly you don't understand.


    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/

  • Learned something new about HAVING.

  • Thanks, i was under the impression that we have to use Having with Group by only and also explanation is clear enough to understand how it works.

  • Excellent Question 🙂

    Great discussion & explanations!

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • THIS THREAD IS DEFINITELY GIVING A LOT TO LEARN.

    SPECIALLY THIS:

    SELECT 'One row'

    WHERE 1 = 0

    HAVING 0 = 0;

Viewing 11 posts - 46 through 56 (of 56 total)

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