HAVING EXISTS

  • I didnt get the exists condition here.. hw it works? can anyone explain me plz?

    Thanks,

    rahul

  • rahul2671985 (9/14/2010)


    I didnt get the exists condition here.. hw it works? can anyone explain me plz?

    Thanks,

    rahul

    Rahul, please follow the entire discussion from page# 1, and I am sure you will get it. Essentially, both the WHERE and HAVING conditions are ultimately evaluated as boolean conditions - if the ultimate result is true, the particular record stays in the result set; else it is discarded.

    What this question also illustrates very well is the logical query processing order, which is also clarified throughout the discussion.

    Thanks & Regards,
    Nakul Vachhrajani.
    http://nakulvachhrajani.com

    Follow me on
    Twitter: @sqltwins

  • rahul2671985 (9/14/2010)


    I didnt get the exists condition here.. hw it works? can anyone explain me plz?

    Thanks,

    rahul

    I second Nakul's suggestion to first read the entire discussion. If, after that, you still struggle to understand the condition, then please post back and indicate exactly where you are stuck. I'll be happy to explain further.


    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/

  • Hi,

    Thanks for replying me. I got stuck after "Having Exists".. the serch condition should take the groups which has max(a.thevalue) greater thaen 3. that I cannot figure out hw it works?

    can u plz explain me in brief?

    Rahul

  • Go back and look at the whole thread. It has been explained in great detail with multiple scripts to run and test. Start on about 2 of the comments and read through the end of page 3. 🙂

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • rahul2671985 (9/15/2010)


    Hi,

    Thanks for replying me. I got stuck after "Having Exists".. the serch condition should take the groups which has max(a.thevalue) greater thaen 3. that I cannot figure out hw it works?

    can u plz explain me in brief?

    Rahul

    I'm not well known for my ability to explain "in brief";-) So I hope you don't mind a long explanation.

    You first have to understand the basics of EXISTS. This condition is mostly used in a WHERE clause. For instance this query (which looks a bit like the one in the question, but is actually different):

    SELECT a.GroupName, a.TheValue

    FROM QotD AS a

    WHERE EXISTS (SELECT *

    FROM QotD AS b

    WHERE b.TheValue > a.TheValue);

    Logically speaking (the actual implementation can be different, but that is beyond our scope for now), SQL Server will process rows from table QotD (aliased as a) one by one. For each row, it will evaluate the subquery, thereby substituting the value of TheValue in the current row for a.TheValue. So for the "first" row (Group 1 / 1), the condition in the subquery will read "WHERE b.TheValue > 1", whereas for the "last" rows (Group 7 / 3), it will read "WHERE b.TheValue > 3".

    After evaluating the subquery, SQL Server checks to see if any rows were returned. If that is the case, the EXISTS clause is considered to be true (there exists at least one row in the result of the subquery), the row passes the check for the conditions in the WHERE clause, so it will be included in the results of the query. If the result of the subquery is empty, the EXISTS condition evalutes to false (no row exists in the subquery) and the "current" row is discarded from the result set of the outer query.

    You'll also have to understand the basics of HAVING. The HAVING clause is very similar to the WHERE clause. The only difference is that it does not operate on individual rows, but on groups of rows as defined by the GROUP BY clause. And that it includes or excludes those groups in their entirety based on the result of the condition evaluation.

    In the case of the query in the original question, groups are made on GroupName - rows with the same GroupName are considered to be in the same group. Because of this grouping, it is still possible to refer to a.GroupName in the HAVING clause (as there can by definition be only one GroupName per group), but you can no longer access individual values of a.TheValue - there might be groups that happen to have only a single value in this column, but there is no guarantee and therefor the syntax rules forbid you to reference a.TheValue (as this would introduce the risk of an ambiguous reference). You can, however, include non grouped columns in an aggregate function. For each group, regardless of the number of rows it contains, MAX(a.TheValue), COUNT(DISTINCT a.TheValue), or AVG(a.TheValue) are an unambiguous reference that can evaluate to only one single value.

    So in short, "HAVING a.TheValue = 2" is illegal (unless you add a.TheValue to the GROUP BY clause, but that would change the semantics); but "HAVING MAX(a.TheValue) = 2" is fine.

    Once you know all these basics, understanding HAVING EXISTS is just a matter of combining the two. The HAVING implies that a condition has to be evaluated for each group (not for individual rows). The EXISTS then says that for each group, the subquery has to be evaluated. But references to the outer table have to be replaced by the values of the current group first. And bacause this is in a HAVING, a reference can only be to a column included in the GROUP BY, or to another column in an aggregate function.

    In the question, MAX(a.TheValue) is the only reference. For the "first" group (Group 1), MAX(a.TheValue) is 3, so the subquery evaluated reads

    EXISTS (SELECT * FROM QotD AS b WHERE b.TheValue > 3)

    (NOTE: The "& gt;" in the code above should be a > symbol, but for some reason the site keeps changing it to the wacky & gt code)

    If this subquery returns rows, the entire group is included in the result set of the outer query; if the subquery returns an empty set, the entire group is excluded from the result set.

    And that's all. Basically, two concepts that are rather familier to most SQL Server developers, used in a very unfamiliar combination.

    Does this help you figure out what happens?


    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/

  • All I can say is WOW. Neat one!

    Jamie

Viewing 7 posts - 31 through 36 (of 36 total)

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