Java Scrap Book  

Java Scrap Book > Database Interview Question > Nth highest Salary for Employee

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.

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.

  1. First find the highest salary in the table.
  2. select max(Salary) from EmployeeSalary;
  3. Write a query to find highest salary of employee by adding condition the highest salary should not be there
  4. select max(Salary) from EmployeeSalary where Salary not in (select max(Salary) from EmployeeSalary);
After running the above query you will get result 5000.

in MySQL you can find Nth Largest salary by below query.
select Salary from Employee Order by Salary desc Limit n-1,1