0

There is an employee table having salary column. How to find the second highest salary among a list of employees?? Is there any convenient way to find the any nth number of salary ??

3 Answers3

1

Oracle has a rank function. Based on the result of this function you can select the nth number.

http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions123.htm

See also this question and answer: Nth max salary in Oracle

Community
  • 1
  • 1
Rene
  • 10,391
  • 5
  • 33
  • 46
0

Second Highest Salary

SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )

Nth Highest Salary

SELECT * 
FROM Employee Emp1
WHERE (N-1) = ( 
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
Ajay Kumar
  • 175
  • 7
0

In Oracle 12c you have function 'NTH_VALUE' you can use for this.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110