第n高的薪水

This commit is contained in:
zeek 2020-04-07 21:37:35 +08:00
parent 52e39dafb4
commit 5bc06e86e6

View File

@ -0,0 +1,29 @@
/**
SQL Employee n Salary
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
Employee n = 2 200 n null
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+
*/
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
SELECT DISTINCT e.salary
FROM employee e
WHERE (SELECT count(DISTINCT salary) FROM employee WHERE salary>e.salary) = N-1
);
END