Database/SQL Server

SQL Server - 릿코드 데이터베이스 문제 176. Second Highest Salary // Leetcode Database Problem

청렴결백한 만능 재주꾼 2022. 7. 27. 03:57
반응형

176. 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

 

테이블 정보 출처:Leetcode.com

이번엔 테이블이 하나가 주어진다.

 

문제는,

Write an SQL query to report the second highest salary from the Employee table. If there is no second highest salary, the query should report null.
Employee 테이블에서 2번째로 높은 샐러리를, 만약 2번째로 높은 샐러리가 없다면 null을 리턴하는 Query 를 작성해라.

 

예시 출처 : Leetcode.com

예시가 문제를 읽을 때 떠오른 것과 똑같다. 

 


풀이,

Rank를  써야하나, 아니면 max를 뽑고 그게 아닌 것 max를 뽑으면 될까 생각든다.

select max(salary) SecondHighestSalary
  from employee 
  where salary <> (select max(salary) from employee)

결과 출처:Leetcode.com

뭔가 10퍼센트가 마음에 안든다.

반응형