본문 바로가기

Mysql/leetcode

[Mysql] 2번째로 높은 값 찾기

반응형
select ifnull((select distinct salary from Employee order by salary desc limit 1,1),null) as SecondHighestSalary 

두번째로 높은 값을 찾기 위해 정렬과 중복값을 제외한다. 그 이후 limit을 통해 두번째 값만을 select로 찾는다

하지만 여기서 고려해야할 사항이 두번째 row가 없을 경우이다.

이 경우를 대비하여 ifnull 안에 select를 넣고 이 값이 null일 경우 null을 출력하게 한다.

 

https://leetcode.com/problems/second-highest-salary/

 

Second 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

 

'Mysql > leetcode' 카테고리의 다른 글

[Mysql] 이전날보다 낮은 온도인 id 찾기  (0) 2020.11.15
[Mysql] 중복된 값 지우기  (0) 2020.11.15
[Mysql] 중복된 값 찾기  (0) 2020.11.12