177. 第N高的薪水 sql

编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。

+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。

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

 1 CREATE FUNCTION getNthHighestSalary ( N INT ) RETURNS INT BEGIN
 2 
 3 DECLARE m INT;
 4 
 5 SET m = N - 1;
 6 
 7 RETURN ( # Write your MySQL query statement below.
 8 
 9 SELECT ifnull( ( SELECT DISTINCT salary FROM Employee ORDER BY salary DESC LIMIT m, 1 ), NULL ) );
10 
11 END

CREATE FUNCTION getNthHighestSalary ( N INT) RETURNS INT BEGIN

DECLARE m INT;

SET m = N-1;

RETURN ( # Write your mysql query statement below.

SELECT ifnull (( SELECT DISTINCT salary FROM Employee ORDER BY salary DESC LIMIT m, 1), NULL ));

END

177. 第N高的薪水 sql

上一篇:微信公众账号开发历程及心得01


下一篇:PhotoShop为老照片翻新修复教程