취미가 좋다

177. Nth Highest Salary 본문

SQL 문제풀이/Leetcode

177. Nth Highest Salary

benlee73 2021. 8. 13. 22:18

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

Solution

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    DECLARE M INT;
    SET M=N-1;
    RETURN (
        # Write your MySQL query statement below.
        SELECT DISTINCT Salary 
        FROM Employee 
        ORDER BY Salary DESC 
        LIMIT M, 1
    );
END
  • DECLARE 를 사용하여 변수 M 을 선언한다.
  • LIMIT 에서 계산을 할 수 없으므로 미리 1을 빼서 M에 넣어준다.
  • LIMIT M, 1 은 M 번째부터 1개 만큼의 데이터를 가져온다는 의미이다.
  • DISTINCT 를 넣어서 n 번째로 큰 급여를 구할 때, 겹치는 것을 제거한다.

'SQL 문제풀이 > Leetcode' 카테고리의 다른 글

178. Rank Scores  (0) 2021.09.06
182. Duplicate Emails  (0) 2021.09.04
181. Employees Earning More Than Their Managers  (0) 2021.09.04
176. Second Highest Salary  (0) 2021.08.13
175. Combine Two Tables  (0) 2021.08.13
Comments