题目描述
Write a SQL query to get the nth highest salary from the Employee
table.
1 | +----+--------+ |
For example, given the above Employee table, the nth highest salary where n = 2 is 200
. If there is no nth highest salary, then the query should return null
.
1 | +------------------------+ |
这里需要使用函数实现查询第N大的salary,考察的是对limit的使用。
limit n, m表示除去前n项后,再输出m项,因此在这里需要limit N-1, 1就可以满足要求。但是由于limit语句中不能做算术操作,因此要将N-1赋值给另外一个变量后M后,再使用limit M, 1。
赋值的时候注意SQL是大小写不敏感的。
代码实现
1 | CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT |