"N"th maximum Salary and SQL Query.

RDBMS from IBM and IBM's Hierarchical DBMS and Transaction Manager (IMS DC).
Post Reply
HafeezAkmal
New Member
Posts: 7
Joined: Tue Apr 01, 2014 8:59 pm

"N"th maximum Salary and SQL Query.

Post by HafeezAkmal »

Hi,

I was just searching for a QUERY which gives me the Nth Max sal of an employee. And found this:

Code: Select all

select max(sal) 
from emp 
where level = 10 
connect by prior sal > sal 
start with sal = (select max(sal) from emp)
I'm confused how this query works? Can somone please help me with this.
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: "N"th maximum Salary and SQL Query.

Post by zprogrammer »

What is the DB2 version at your shop?

And what happened when you tried to execute this as a DB2 query?
zprogrammer
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: "N"th maximum Salary and SQL Query.

Post by Anuj Dhawan »

Those are Oracle constructs and this is an IBM DB2 part of the Forum, so I'll assume that you're looking forward to a DB2 solution, are you?
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
HafeezAkmal
New Member
Posts: 7
Joined: Tue Apr 01, 2014 8:59 pm

Re: "N"th maximum Salary and SQL Query.

Post by HafeezAkmal »

Thanks Anuj. Yes, I need the answer for DB2. Please help.

We're at DB2 10. I can not access the mainframe as of now but the error was something that keyword is not recognized.
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: "N"th maximum Salary and SQL Query.

Post by Anuj Dhawan »

Try:

Code: Select all

SELECT *                                         
FROM EMPLOYEE E1                              
      WHERE 10 =                                  
      (SELECT COUNT(DISTINCT E2.SALARY) FROM     
       EMPLOYEE E2 WHERE E2.SALARY>=E1.SALARY)
With DB2 10, you can make use of FETCH FIRST N ROWs also.
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: "N"th maximum Salary and SQL Query.

Post by zprogrammer »

IIRC , You could generate the row id...
zprogrammer
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: "N"th maximum Salary and SQL Query.

Post by Anuj Dhawan »

Right, and DB2 has to be in NFM for that...unless 'am mistaken.
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
HafeezAkmal
New Member
Posts: 7
Joined: Tue Apr 01, 2014 8:59 pm

Re: "N"th maximum Salary and SQL Query.

Post by HafeezAkmal »

Thanks Anuj and Pandora. I used your query and it's working Anuj!

Thank you! :)

By the way, With ROW-ID, will there be an additional overhead, just thinking...
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM DB2 and IMS DB/DC”