Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


HAVING EXISTS


HAVING EXISTS

Author
Message
Rahul26
Rahul26
SSC Journeyman
SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

Group: General Forum Members
Points: 96 Visits: 139
I didnt get the exists condition here.. hw it works? can anyone explain me plz?

Thanks,
rahul
Nakul Vachhrajani
Nakul Vachhrajani
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1824 Visits: 2127
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
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8321 Visits: 11562
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 MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Rahul26
Rahul26
SSC Journeyman
SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

Group: General Forum Members
Points: 96 Visits: 139
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16562 Visits: 17010
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8321 Visits: 11562
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 MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Jamie-2229
Jamie-2229
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2955 Visits: 831
All I can say is WOW. Neat one!

Jamie
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search