최근 글 ✨

[LeetCode] 177. Nth Highest Salary

문제

Write a solution to find the nth highest distinct salary from the Employee table. If there are less than n distinct salaries, return null.

 

Employee 테이블에서 n번째로 높은 연봉을 출력한다. 없으면 null을 출력한다.

 

 

쿼리

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N=N-1;
  RETURN (
    SELECT DISTINCT SALARY FROM EMPLOYEE ORDER BY SALARY DESC LIMIT N,1
  );
END

 

null 처리를 위해서 Distinct를 사용해줬고 하나만 출력하면 돼서 limit를 사용했다.

오류가 나서 discussion을 보니까 leetcode 오류로 n=n-1을 설정해줘야 한다고 해서 다시 처리를 해줬다.

 

 

 

문제 출처

https://leetcode.com/problems/nth-highest-salary/description/

'Study > SQL' 카테고리의 다른 글

[LeetCode] 182. Duplicate Emails  (0) 2025.11.25
[LeetCode] 180. Consecutive Numbers  (0) 2025.11.19
[LeetCode] 178. Rank Scores  (0) 2025.11.13
[LeetCode] 176. Second Highest Salary  (0) 2025.11.06
[Programmers] SELECT(1)  (1) 2024.08.30