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
 

 

Query to find nth max salary in table EMP

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

New User


Joined: 02 Nov 2009
Posts: 73
Location: Mumbai

PostPosted: Fri Feb 12, 2010 2:14 pm    Post subject: Query to find nth max salary in table EMP
Reply with quote

Hi,

Can Anyone please explain me the logic behind the below query to find the nth max salary in EMP table -

Quote:
select sal from emp A where n =

( select count(*) from emp B where A.sal < B.sal )





Thanks
Back to top
View user's profile Send private message

enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10274
Location: italy

PostPosted: Fri Feb 12, 2010 2:16 pm    Post subject: Reply to: Query to find nth max salary in table EMP
Reply with quote

topic moved where it belongs!
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Fri Feb 12, 2010 2:28 pm    Post subject:
Reply with quote

Hello,

Suggest you write down 5 "salaries" and manually step thru the process.

This will quickly show what happens when the query processes.
Back to top
View user's profile Send private message
anshul_gugnani

New User


Joined: 02 Nov 2009
Posts: 73
Location: Mumbai

PostPosted: Fri Feb 12, 2010 3:21 pm    Post subject:
Reply with quote

Hi
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Fri Feb 12, 2010 10:38 pm    Post subject: Reply to: Query to find nth max salary in table EMP
Reply with quote

Quote:
Hi
Well, it is a start. . . icon_confused.gif
Back to top
View user's profile Send private message
anshul_gugnani

New User


Joined: 02 Nov 2009
Posts: 73
Location: Mumbai

PostPosted: Fri Feb 12, 2010 11:15 pm    Post subject:
Reply with quote

Hi, Say I have 5 salaries as -

12, 25, 13, 78, 03

According to
Quote:
select count(*) from emp B where A.sal < B.sal


each salary will have a count for which it is greater then other salaries in table.

So result for records will be this will be -

3, 1, 3, 0, 4

Now, According to
Quote:
select sal from emp A where (n-1) =



25 is salary whose count is equal to (n-1).

Please suggest if my understanding is correct. I think this co-related subquery.

Thanks!
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Sat Feb 13, 2010 3:01 am    Post subject:
Reply with quote

Hello,

Other than the typo (3, 1, 3, 0, 4) , i believe you have it correctly icon_smile.gif

Also - Fromthe db2 manual (i added the bold for emphasis):
Quote:
A subquery is actually executed for each row of R only if it includes a correlated reference. In fact, a subquery with no correlated references is executed just once, whereas a subquery with a correlated reference may have to be executed once for each row.
Back to top
View user's profile Send private message
anshul_gugnani

New User


Joined: 02 Nov 2009
Posts: 73
Location: Mumbai

PostPosted: Sat Feb 13, 2010 3:15 pm    Post subject:
Reply with quote

Hi,

Quote:
Other than the typo (3, 1, 3, 0, 4) ,


Could you please explain as to why this is wrong??

Thanks
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Sat Feb 13, 2010 3:57 pm    Post subject:
Reply with quote

Hello,

Maybe 3, 1, 2, 0, 4 . . .?
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 Loading data to table gives wrong for... Raghu navaikulam DB2 16 Thu Jul 13, 2017 2:11 pm
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts find RACF group for access to spooled... jzhardy JCL & VSAM 1 Mon May 08, 2017 11:46 am
No new posts To find out size allocated to a seque... ashek15 JCL & VSAM 15 Thu Apr 27, 2017 9:42 am
No new posts unload data from table with lob columns farhad_evan DB2 1 Sat Apr 22, 2017 1:32 pm


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