How to find n'th highest value of a column?

2020-07-05 05:17发布

Is there a command akin to:

  • 2nd highest salary from tbl_salary or

  • 4th highest salary from tbl_salary ?

I've seen:

select salary
from tbl_salary t
where &n = (
    select count(salary) 
    from(
        select distinct salary
        from tbl_salary
    )where t.salary<=salary
);

How does this it works?

Are there other simple ways to get result?

标签: mysql
8条回答
萌系小妹纸
2楼-- · 2020-07-05 06:19

Simplest Implementation,

 (select * from tbl_salary order by salary desc limit 5) order by salary limit 1;


 (select * from tbl_salary order by salary desc limit 2) order by salary limit 1;
查看更多
Explosion°爆炸
3楼-- · 2020-07-05 06:23

SELECT sal from emp order by sal desc limit 1,1

查看更多
登录 后发表回答