QOD Jan 6 2004

  • /rant

    Is it just me, or is the question totally wrong for the answers that are presented? It makes no sense. Sure, you can figure out what the question should be, but we shouldn't have to do that. Just a little frustrated this morning and thought I would vent.

    /end rant

  • Cliffb -

    Breathe in...Breathe Out (sorry, that is just what others tell me when I start ranting)

    I liked the question (except for the mispelling).  I know when I should use the HAVING clause, but I wouldn't be able to tell someone what it does...so this one prompted me to open books online and get a refresher.

    Michelle



    Michelle

  • From BOL:

    <Quote>

    The WHERE and HAVING clauses in a SELECT statement control the rows from the source tables that are used to build the result set. WHERE and HAVING are filters. They specify a series of search conditions, and only those rows that meet the terms of the search conditions are used to build the result set.

    The HAVING clause is typically used in conjunction with the GROUP BY clause, although it can be specified without GROUP BY.

    </Quote>

    So technically I think HAVING is exactky the same as a WHERE clause.

     


    Joseph

  • I was ranting about the question which was: What is the best explaination of why you would use the HAVING clause in T-SQL?  and has nothing to do with the answers. The answers refered more to how you would use the HAVING clause, not WHY you would use it.

    In reference to the HAVING and WHERE being the same, they are similar, but I believe you can only use aggregates in the HAVING clause.

  • I aggree with cliffb

    In fact the correct answer should be Sets conditions to a SELECT query, usually use after a GROUP BY clause

     

  • From BOL:

    The HAVING clause sets conditions on the GROUP BY clause similar to the way WHERE interacts with SELECT. The WHERE search condition is applied before the grouping operation occurs; the HAVING search condition is applied after the grouping operation occurs. The HAVING syntax is similar to the WHERE syntax, except HAVING can contain aggregate functions. HAVING clauses can reference any of the items that appear in the select list.

    Therefore I think the question is correct:

    "Sets conditions to the query after a GROUP BY clause"

     

    Make sense to me anyways.

  • You are right for the application order, WHERE condition is applied before grouping and HAVING condition is applied after and both WHERE and HAVING clauses can be present at the same time in a SELECT query. But you can use a HAVING clause without any GROUP BY clause and in this case you can only have aggregate function in SELECT and in HAVING clauses.

    Here is the T-SQL reference of HAVING according to MSDN

    ***********

    HAVING

    Specifies a search condition for a group or an aggregate. HAVING can be used only with the SELECT statement. It is usually used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause. For more information, see SELECT.

    ***********

    So, to me, "right answer" is not correct but it's my opinion

     

  • I agree with you as well, I think both views of this question are correct.

    Having with where, without where, with group by, without group by are all valid options.

    However what I was saying that giving the question "what is the best answer" and giving the availble answers to select. The answer to the question is right.

     

     

  • What a can of worms I opened =)

    I still have to disagree that the question was phrased incorrectly. Again, the question was What is the best explaination of why you would use the HAVING clause in T-SQL?

    None of the answers relate to why you would use the HAVING clause, but rather how you would use it and where it should be applied syntactically.

Viewing 9 posts - 1 through 8 (of 8 total)

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