a Sub-Query and a Correlated Sub-Query

Ask question on - DataBase Concepts, IBM Mainframe DB2, DB2/UDB, DB2 Tools, SQL , DB2 programming with languages like COBOL, PL1.

Moderators: Kalicharan, Moderator Group

Post Reply
User avatar
arrbee
Active Member
Posts: 144
Joined: Fri Feb 24, 2006 11:33 am

a Sub-Query and a Correlated Sub-Query

Post by arrbee » Sat Sep 12, 2009 9:38 am

Hi,

Would you please tell me how a sub-query looks like and a correlated sub-query? Do they always have fixed form of writing it?

Thanks.
Arr Bee
-------------
?My joy in learning is partly that it enables me to teach? - Seneca(Roman philosopher, mid-1st century AD)

User avatar
Natarajan
Moderator
Posts: 537
Joined: Fri Oct 10, 2008 12:57 pm
Location: chennai
Contact:

sub query vs correlated sub query

Post by Natarajan » Sat Sep 12, 2009 4:02 pm

Hi,

You can refer correlated subquery in
http://www.mainframegurukul.com/Mainfra ... php?t=4628

----------
Example
----------

Subquery -

Code: Select all

SELECT * FROM  TABLE  WHERE
FIELD1 = ( SELECT FIELDA FROM TABLEB )
Correlated suquery -

Code: Select all

SELECT * FROM  TABLE  A  WHERE
FIELD1 = ( SELECT FIELDA FROM TABLEB  B  WHERE A.FIELD1 = B.FIELDB )
Here we are using instance A from outer query in the innerquery, hence we are calling it as correlated subquery.


In normal subquery, we dont have any reference from outer query, being used in the inner query.
Natarajan
Chennai

User avatar
arrbee
Active Member
Posts: 144
Joined: Fri Feb 24, 2006 11:33 am

inner query?

Post by arrbee » Tue Sep 22, 2009 2:23 pm

Hi, Thanks for the reply.

Does inner query execute first or outer query in sub-query / correlated sub-query?

Please explain.

Thanks.
Arr Bee
-------------
?My joy in learning is partly that it enables me to teach? - Seneca(Roman philosopher, mid-1st century AD)

User avatar
Natarajan
Moderator
Posts: 537
Joined: Fri Oct 10, 2008 12:57 pm
Location: chennai
Contact:

Post by Natarajan » Tue Sep 22, 2009 3:08 pm

subquery -
inner query executes first then outer query.

correlated subquery -
outer query starts executing first , inner query executes once for every record fetched by outer query.
Natarajan
Chennai

Post Reply

FREE TUTORIALS

Tutorials
Free tutorials from mainframegurukul
  • JCL Tutorial
    Covers all important JCL concepts.
  • Cobol Tutorial
    This tutorials covers all Cobol Topics from STRING to COMP-3.
  • DB2 Tutorial
    DB2 Tutorial focuses on DB2 COBOL Programming.
  • SORT Tutorial
    This Tutorial covers all important aspects of DFSORT with examples
  • CICS Tutorial
    This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions



Other References
Mainframe Tools and others