LeetCode Nth Highest Salary
2015-04-12 22:58
447 查看
Write a SQL query to get the nth highest salary from the
For example, given the above Employee table, the nth highest salary where n = 2 is
If there is no nth highest salary, then the query should return
注意相同的salary算一位,而且limit以及offset不能用表达式,至少我写的时候是不行的。
一开始不知道如何声明一个变量,结果硬是写了这个方法:
Employeetable.
+----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+
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.
注意相同的salary算一位,而且limit以及offset不能用表达式,至少我写的时候是不行的。
一开始不知道如何声明一个变量,结果硬是写了这个方法:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN RETURN ( select(select a.salary from(select distinct b.salary from Employee b union all select max(c.salary) from Employee c)a order by a.salary desc limit 1 offset N) ); END后来在discuss看到了如何声明变量:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN declare M int; set M = N - 1; RETURN ( select distinct salary from Employee order by salary desc limit 1 offset M ); END
相关文章推荐
- 【LeetCode】Second Highest Salary && Nth Highest Salary
- [LeetCode]-DataBase-Nth Highest Salary
- LeetCode | Second Highest Salary & Nth highest salary
- leetcode 177 Nth Highest Salary
- Leetcode | Database | Nth Highest Salary
- [LeetCode][SQL]Nth Highest Salary
- LeetCode-Algorithms #003 Longest Substring Without Repeating Characters, Database #177 Nth Highest Salary
- LeetCode - Nth Highest Salary
- LeetCode_OJ【177】Nth Highest Salary
- nth highest salary leetcode
- leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)
- [LeetCode] Nth Highest Salary 第N高薪水
- leetcode177-Nth Highest Salary(找出第n大的数据)
- [LeetCode]Nth Highest Salary,解题报告
- LeetCode 176. Second Highest Salary
- 【LeetCode】Department Highest Salary && Department Top Three Salaries
- LeetCode Department Highest Salary
- leetcode 176. Second Highest Salary
- leetcode 176. Second Highest Salary
- Leetcode176. Second Highest Salary