Find nth highest Salary of Employee in the database
This is very common question ask in all the database interview question and it is little bit tricky. Suppose you have a employee salary table which have only two column. employee id and employee salary.
Write a SQL query to get the second highest employee salary from the above table.
We should follow following approach to solve this problem.
- First find the highest salary in the table. select max(Salary) from EmployeeSalary;
- Write a query to find highest salary of employee by adding condition the highest salary should not be there select max(Salary) from EmployeeSalary where Salary not in (select max(Salary) from EmployeeSalary);
select Salary from Employee Order by Salary desc Limit n-1,1