How to get nᵗʰ highest value using plain SQL

前端 未结 8 1086
孤城傲影
孤城傲影 2021-01-24 23:42

What is the simplest way to get the nth highest value from a result set using plain SQL?

The result set would be huge, thus need to consider performance too.

8条回答
  •  无人共我
    2021-01-25 00:11

    Sub Query will list out top 'n' highest salary values. From that list minimum value will be the nth highest salary.

    `SELECT min(salary) FROM
        (SELECT DISTINCT TOP n salary FROM EmployeeTable ORDER BY salary desc);`
    

    Eg :- SQL query to find third maximum salary

    `SELECT min(salary) FROM
        (SELECT DISTINCT TOP 3 salary FROM EmployeeTable ORDER BY salary desc);`
    

提交回复
热议问题