Get Nth Highest Salary
Write an SQL query to report the
nth
highest salary from theEmployee
table. If there is nonth
highest salary, the query should reportnull
.The query result format is in the following example.
CREATE FUNCTION getNthHighestSalary ( N INT ) RETURNS INT BEGIN
DECLARE
var INT;
SET var = N - 1;
RETURN (
SELECT
(
SELECT DISTINCT
Salary
FROM
Employee
ORDER BY
Salary DESC
LIMIT 1 OFFSET var
) AS SecondHighestSalary
);
END;
SELECT
getNthHighestSalary ( 2 );
- 由前一題改變而來
- OFFSET 改 var