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

Need some help in understanding the query


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

New User


Joined: 08 Dec 2009
Posts: 15
Location: Mumbai

PostPosted: Mon Jul 30, 2012 5:59 pm
Reply with quote

I am facing a problem understanding the out put of a DB2 query. I’ll briefly give u the table columns and then the query.
The table is an Employee table with:
1st Col: Dept Num
2nd Col: Employee ID
3rd Col: Employee Last Name
4th Col: Employee First Name
…. The rest of the cols I am not including since the query is restricted to these columns.

I ran the following query 1st:
Code:
Select Dept_Num, Employee_ID, Emp_Lst_Nam, Emp_Fst_Nam
From DXXX.TBWP_EMP
Where Emp_Lst_Nam IN (‘PANCAKE’);

The output was:

Code:
---------+---------+---------+---------+---------+---------+---------+
DEPT_NUM  EMPLOYEE_ID  EMP_LST_NAM      EMP_FST_NAM   
---------+---------+---------+---------+---------+---------+---------+
      14.     90440.   PANCAKE          MEREDITH               
     458.      1641.   PANCAKE          MARY                   
DSNE610I NUMBER OF ROWS DISPLAYED IS 2


I ran the following query next:
Code:
Select Dept_Num, Employee_ID, Emp_Lst_Nam, Emp_Fst_Nam
From DXXX.TBWP_EMP
Where Employee_ID IN ( Select Employee_ID from DXXX.TBWP_EMP
                                           Where Emp_Lst_Nam IN (‘PANCAKE’))

The output was:

Code:
---------+---------+---------+---------+---------+---------+
DEPT_NUM  EMPLOYEE_ID  EMP_LST_NAM      EMP_FST_NAM   
---------+---------+---------+---------+---------+---------+
      14.     90440.   PANCAKE          MEREDITH
     458.      1641.   PANCAKE          MARY     
     881.      1641.   BENTLEY          HILMA                   
    3885.      1641.   WALLACE          SARA     
    4022.      1641.   LEMAHIEU         BARBARA   
    4138.      1641.   LOMELI           ESTHER     
DSNE610I NUMBER OF ROWS DISPLAYED IS 6

I am not getting how is the 2nd query working to produce such result set. Can anyone explain how the 2nd query is working?

Coded - Anuj
Back to top
View user's profile Send private message
Gnanas N

Active Member


Joined: 06 Sep 2007
Posts: 792
Location: Chennai, India

PostPosted: Mon Jul 30, 2012 6:10 pm
Reply with quote

Perhaps, someone inserted some rows after your first query execution, before second query run.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Mon Jul 30, 2012 6:17 pm
Reply with quote

You have multiple employee_id's with different emp_lst_name. employee_id '1641' is used for Bentley, Wallace, Lemahieu, and Lomeli. Your first query is based on emp_lst_name but your second is based on employee_id (which is not unique across dept_num's)
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Mon Jul 30, 2012 9:30 pm
Reply with quote

your first query returns two EmpIds : 90440 and 1641
your second query returns all rows with those empids

maybe what you wanted to write was :
Code:
Select Dept_Num, Employee_ID, Emp_Lst_Nam, Emp_Fst_Nam
From DXXX.TBWP_EMP
Where (dept_num,Employee_ID) IN ( Select Dept_num,Employee_ID from DXXX.TBWP_EMP
                                           Where Emp_Lst_Nam IN (‘PANCAKE’))
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 RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts Issue with EXEC CICS QUERY SECURITY c... CICS 6
Search our Forums:

Back to Top