Solution to finding the 2nd highest salary in SQL
SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )
Finding nth highest salary example
SELECT *
FROM Employee Emp1
WHERE (1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
FROM Employee Emp1
WHERE (1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
Find the nth highest salary in Oracle using rownum
select * from (
select Emp.*,
row_number() over (order by Salary DESC) rownumb
from Employee Emp)
where rownumb = n; /*n is nth highest salary*/
select Emp.*,
row_number() over (order by Salary DESC) rownumb
from Employee Emp)
where rownumb = n; /*n is nth highest salary*/
Reference:-
No comments:
Post a Comment