Algorithm/SQL
[SQL][Leetcode] 177. Nth Highest Salary
융무
2020. 6. 19. 22:43
https://leetcode.com/problems/nth-highest-salary/
Nth Highest Salary - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Problem
Analysis
1. 먼저 N번째 높은 salary리를 찾는것이기 때문에 ORDERY BY를 이용해서 DESC 한다
2. OFFSET은 몇번째 ROW부터 출력할 것인지 묻는 함수이다 파이썬의 순서에 0,1,2를 생각하면 편할거같다.
Solution
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N = N-1;
RETURN (
# Write your MySQL query statement below.
SELECT (
SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET N
)
);
END