문제
Write a solution to find the second highest distinct salary from the Employee table. If there is no second highest salary, return null (return None in Pandas).
Employee 테이블에서 두번째로 높은 연봉을 출력한다. 만약 없다면 null을 출력한다.
쿼리
SELECT
MAX(a.salary) AS SecondHighestSalary
FROM
(
SELECT
salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS r
FROM
Employee
) a
WHERE
a.r = 2;
null 처리를 위해서 ifNull을 활용했는데 제대로 동작을 안했다.
찾아보니까 ifNull은 행이 없을 때는 동작하지 않는다.
서브쿼리에서 조회된 데이터가 없으면 null이고 뭐고 표시가 안된다.
그래서 MAX를 사용해서 데이터가 없을 때에도 null이 표시되도록 작성해주었다.
문제 출처
https://leetcode.com/problems/second-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] 177. Nth Highest Salary (0) | 2025.11.10 |
| [Programmers] SELECT(1) (1) | 2024.08.30 |