How to find third or nᵗʰ maximum salary from salary table?

后端 未结 30 756
醉酒成梦
醉酒成梦 2020-11-30 16:30

How to find third or nth maximum salary from salary table(EmpID, EmpName, EmpSalary) in optimized way?

30条回答
  •  广开言路
    2020-11-30 17:13

    Too simple if you use the sub query!

    SELECT MIN(EmpSalary) from (
    SELECT EmpSalary from Employee ORDER BY EmpSalary DESC LIMIT 3
    );
    

    You can here just change the nth value after the LIMIT constraint.

    Here in this the Sub query Select EmpSalary from Employee Order by EmpSalary DESC Limit 3; would return the top 3 salaries of the Employees. Out of the result we will choose the Minimum salary using MIN command to get the 3rd TOP salary of the employee.

提交回复
热议问题