Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Correlated subquery -- higher level of abstraction

Author  Topic 

kmansoor
Starting Member

2 Posts

Posted - 2011-12-30 : 12:54:01
Hi All- I understand a correlated query uses values from the outer/containing query.

Joe Celko, in 'SQL for smarties' says it is a way to hide loops in SQL.

My question: What kind of problems are generally solved using correlated subqueries? is there a 'way' for me to discern 'right away' this problem will need a correlated sub-query to solve?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-30 : 17:10:28
I don't know if there is a litmus test that can let one discern whether to use subqueries or not. But, subqueries are useful when you want to evaluate something for each row in the outer query - a brief description on MSD alludes to that: http://msdn.microsoft.com/en-us/library/ms187638.aspx

After I read your posting, I was thinking about how *I* decide how to write a query; i.e., how I decide what features of T-SQL to use, or what methodology to use. I am really unable to answer it - I just don't know. When I see the problem, I sort of know what technique might fit that problem, and go with that. And a lot of times, that "shooting from the hip" approach ends up being not the best approach. So then I rework it to make it better.

More experienced and skilled SQL experts on this forum may have systematic ways of approaching a problem. If they do, I don't know what those are.
Go to Top of Page

kmansoor
Starting Member

2 Posts

Posted - 2011-12-30 : 22:35:50
Hi- Thank you for replying, one clarification:

What type of problems a 'Correlated Sub-query' solves? when would one use a correlated sub-query instead of a regular sub-query?

Go to Top of Page
   

- Advertisement -