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 ??
Asked
Active
Viewed 74 times
0
-
Please post the DDL statements for your tables, example input, expected output plus the queries you've tried so far. – Frank Schmitt Feb 10 '14 at 07:45
3 Answers
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
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