May 03, 2022 6:12 am
Here you go. How to write an SQL query to find the 3rd highest salary from an employee table?
Input
emp_name emp_sal
Ramu 1000
Ramesh 4000
Peter 3000
Output
emp_name emp_sal
Ramesh 4000
SQL Query 1
select * from( select emp_name, emp_sal, dense_rank() over(order by emp_sal desc)t from Employee) where t=&n; To find the 2nd highest sal set n = 2 To find the 3rd highest sal set n = 3 and so on.
How do find out the Top 3 Employees based on their Salary?
Input
emp_num emp_name emp_sal
1 John 5000
2 Peter 2000
3 Allen 9000
5 Merlin 7000
Output
emp_num emp_name emp_sal
1 Allen 9000
2 Merlin 7000
3 John 5000
SQL Query2
With Results As ( Select Emp_num, emp_name,emp_sal, Dense_Rank() Over (Order by emp_sal Desc) As Dense_Rank ) Select * from Results where Results.Dense_Rank<=3