[SQL]LeetCode177. 第N高的薪水 | Nth Highest Salary

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-vcymjmog-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Write a SQL query to get the nth highest salary from the Employee table.git

+----+--------+
| 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.github

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+

編寫一個 SQL 查詢,獲取 Employee 表中第 高的薪水(Salary)。微信

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如上述 Employee 表,n = 2 時,應返回第二高的薪水 200。若是不存在第 高的薪水,那麼查詢應返回 nullspa

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+

140ms
1 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
2 BEGIN
3     set N = N-1;
4   RETURN (
5       # Write your MySQL query statement below.
6       select distinct Salary from Employee order by Salary desc limit N,1
7   );
8 END

149mscode

 1 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
 2 BEGIN
 3   set N = N-1;
 4   RETURN (      
 5       select distinct Salary as 'getNthHighestSalary(2)'
 6       from Employee
 7       order by Salary desc
 8       limit N, 1
 9   );
10 END
相關文章
相關標籤/搜索