leetCode/sql/getNthHighestSalary.sql

30 lines
748 B
MySQL
Raw Normal View History

2020-04-07 13:37:35 +00:00
/**
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