【題目】
編寫(xiě)一個(gè) SQL 查詢(xún)激率,獲取 Employee
表中第 n 高的薪水(Salary)。
Id | Salary |
---|---|
1 | 100 |
2 | 200 |
3 | 300 |
例如上述 Employee
表勿决,n = 2 時(shí)乒躺,應(yīng)返回第二高的薪水 200
。如果不存在第 n 高的薪水低缩,那么查詢(xún)應(yīng)返回 null
嘉冒。
getNthHighestSalary(2) |
---|
200 |
【題解】
select distinct salary
from
(select dense_rank() over(order by salary desc) as rk,salary
from employee) a
where a.rk=n;