How to Select and display the column data from Inner queries

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
charigln
Member
Posts: 5
Joined: Thu May 22, 2014 11:55 am

How to Select and display the column data from Inner queries

Post by charigln » Thu Aug 14, 2014 6:44 pm

Hi,

I want to Display the data from Inner queries of co-related sub query. Below is what I wish to do...

Select Emp1 from EMP
where empcode IN (select empcd from DEPT
where Empfrstname like 'Sures%');

I want to add empcd from DEPT table to be displayed along with Emp1 of EMP table, I don't want to display empcode from EMP table. how can I do it?

My result set should display values of both Emp1 from table - EMP and empcd from DEPT table.

Guys, pls don't expect production code from me. Above is just a example.

cyte
Member
Posts: 2
Joined: Fri Aug 15, 2014 10:42 am

Post by cyte » Fri Aug 15, 2014 11:07 am

The below SQL should provide the same result.


Select Emp1, empcode
from EMP
where empcode in (select empcd from DEPT
where empfrstname like 'Sures%') ;


But, if you want to get a different field from DEPT other the EMPCD, then you can use the below SQL.

Select E.Emp1, D.FIELD2
from emp E
left join Dept D
on E.empcode = D.empcd
Where d.empfrstname like 'Sures%';

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