Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

what is the diff b/w correlated query & non-correlated q

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
bhaskar80

New User


Joined: 12 Apr 2007
Posts: 7
Location: bangalore

PostPosted: Mon Apr 16, 2007 6:00 pm    Post subject: what is the diff b/w correlated query & non-correlated q
Reply with quote

what is the diff b/w correlated query & non-correlated query? which is faster ? why?
Back to top
View user's profile Send private message

mkk157

Active User


Joined: 17 May 2006
Posts: 310

PostPosted: Wed Apr 18, 2007 2:49 pm    Post subject: Hi
Reply with quote

Hi Bhaskar,

For Info about the Correlated Sub-queries see the following link http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/sqlp/rbafycorrs.htm
Back to top
View user's profile Send private message
mkk157

Active User


Joined: 17 May 2006
Posts: 310

PostPosted: Wed Apr 18, 2007 2:52 pm    Post subject: Hi
Reply with quote

A correlated subquery is a subquery that is evaluated once for each row of the outer query.

It may indeed be convenient to imagine a correlated subquery as being "evaluated" or "executed" once for each row of the outer query; do not, however, be misled into thinking that the database optimizer actually executes it in that fashion, because this will lead you to believe that it's not very efficient, when in fact most optimizers will process a correlated subquery very efficiently, as a join.

Okay, with that out of the way, what's the difference between an ordinary subquery and a correlated subquery? The correlated subquery makes an actual reference, using a correlation variable, to the outer query. For example, here's a query with two subqueries:

select studentname
, studentmark
, ( select avg(studentmark)
from students
where class = t1.class ) as classaverage
, ( select avg(studentmark)
from students ) as schoolaverage
from students t1
In the above example, t1 is the correlation variable that lets the correlated subquery refer to the table in the outer query. For each student, two averages are calculated: the average mark of all students in the same class, and the overall average of all students in all classes.

Here's another example of a correlated subquery, this time in the WHERE clause:

select category
, articletitle
, articlepubdate
from articles zz
where articlepubdate
= ( select max(articlepubdate)
from articles
where category = zz.category )
Back to top
View user's profile Send private message
bhaskar80

New User


Joined: 12 Apr 2007
Posts: 7
Location: bangalore

PostPosted: Thu Apr 19, 2007 1:54 pm    Post subject: Re: what is the diff b/w correlated query & non-correlat
Reply with quote

Thanks a lot.. sir..
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us