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

后端 未结 30 725
醉酒成梦
醉酒成梦 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:11

    select * from employee order by salary desc;
    
    +------+------+------+-----------+
    | id   | name | age  | salary    |
    +------+------+------+-----------+
    |    5 | AJ   |   20 | 100000.00 |
    |    4 | Ajay |   25 |  80000.00 |
    |    2 | ASM  |   28 |  50000.00 |
    |    3 | AM   |   22 |  50000.00 |
    |    1 | AJ   |   24 |  30000.00 |
    |    6 | Riu  |   20 |  20000.00 |
    +------+------+------+-----------+
    
    
    
    
    select distinct salary from employee e1 where (n) = (select count( distinct(salary) ) from employee e2 where e1.salary<=e2.salary);
    

    Replace n with the nth highest salary as number.

提交回复
热议问题