IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Join VS Subquery


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
richie_techin

New User


Joined: 21 Jan 2006
Posts: 16
Location: india

PostPosted: Tue Jan 31, 2006 11:10 am
Reply with quote

hi,

Can anyone tell me why the JOIN is more preffered than SUBQUERY?
what is the difference between SUBQUERY andf CO-RELATED SUBQUERY?
Back to top
View user's profile Send private message
Hames

New User


Joined: 03 Oct 2005
Posts: 49

PostPosted: Tue Jan 31, 2006 4:35 pm
Reply with quote

Quote:
what is the difference between SUBQUERY andf CO-RELATED SUBQUERY?


Sub query is the better than correlated subquery in the following manner.

1> sub queries are much faster.
2> Subqueries follow bottom up approach where as correlated subqueries follow both top-down as well as bottom up approach.
3> correlated subqueries normally used for querying in the same table like finding the second maximum salary or n th maximum salary in the employees table.
4> subqueries will first execute then the control given to the main query.

Where as in Correlated subquery, first a value is selected from main query then the sub query will be executed for all rows in subquery table. Then a second value is selected from the main query again sub query will be executed for all rows in subquery table. and upto the last record available in main query it goes on the same way.

That's why correlated subqueries are taking much time.
Back to top
View user's profile Send private message
Hames

New User


Joined: 03 Oct 2005
Posts: 49

PostPosted: Tue Jan 31, 2006 4:45 pm
Reply with quote

Quote:
Can anyone tell me why the JOIN is more preffered than SUBQUERY?


For subqueries only one option is available that's whatever condition you give, you get the results for that condition.

But in the case of JOIN we have two type of joins. INNER JOIN and OUTER JOIN.

INNER JOIN is simply like the same as a subquery, it will give the result for the given condition.

In OUTER JOIN we have three types. (I) LEFT OUTER JOIN (II) RIGHT OUTER JOIN (III) FULL OUTER JOIN

In LEFT OUTER JOIN, you will get the results for the given condition in addition to that the remaining rows in the first table which are not satisfied the condition also will be included in the result.

In RIGHT OUTER JOIN, you will get the results for the given condition in addition to that the remaining rows in the Second table which are not satisfied the condition also will be included in the result.

In FULL OUTER JOIN, you will get the results for the given condition in addition to that the remaining rows in both the table which are not satisfied the condition also will be included in the result.

If you go thru an example you will easily understand these concepts. Go thru the tutorials for example. IF you did not fine let me know I will send you the simple examples.

You can check this details in IBM DB2 UDB V 8.1 CERTIFICATION 700 Book written by Roger Sanders
Back to top
View user's profile Send private message
nuthan

Active User


Joined: 26 Sep 2005
Posts: 146
Location: Bangalore

PostPosted: Tue Jan 31, 2006 7:39 pm
Reply with quote

Hi,
In subquery first the inner query will execute, then based on the result of inner query outerquery will execute.

In correlated subquery for each row of outer query inner query will execute one time.
Back to top
View user's profile Send private message
Hames

New User


Joined: 03 Oct 2005
Posts: 49

PostPosted: Wed Feb 01, 2006 5:39 pm
Reply with quote

Quote:
In correlated subquery for each row of outer query inner query will execute one time.


Hi Nuthan,

This statement is not correct.

In correlated subquery for each row of outer query inner query will execute all rows in the table specified in the inner query.
Back to top
View user's profile Send private message
nuthan

Active User


Joined: 26 Sep 2005
Posts: 146
Location: Bangalore

PostPosted: Wed Feb 01, 2006 7:06 pm
Reply with quote

Hi ,
i told 'one time' in the sence like one time of all the rows which will satisfy the inner query. Any how thanks for correcting my sentence.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Join multiple records using splice DFSORT/ICETOOL 5
No new posts Join 2 files according to one key field. JCL & VSAM 3
No new posts JOIN STATEMENT PERFORMANCE. DFSORT/ICETOOL 12
No new posts Join files where value in one is betw... DFSORT/ICETOOL 6
No new posts Is the Output of Sort/Join dependent ... DFSORT/ICETOOL 2
Search our Forums:

Back to Top