ror_coding

[LeetCode] 176. Second Highest Salary 본문

Coding Test/SQL

[LeetCode] 176. Second Highest Salary

ro_rdil_31 2025. 7. 13. 16:25
728x90

limit 1 offset 1; <- 2번째로 높은 값을 뽑기 위해 offset을 사용합니다.

 

Question

 

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

The result format is in the following example.

 

Table: Employee

 

+-------------+------+
| Column Name | Type |
+-------------+------+
| id          | int  |
| salary      | int  |
+-------------+------+
id is the primary key (column with unique values) for this table.
Each row of this table contains information about the salary of an employee.

 

Point

 

  • limit 1 offset 1
    • offset 은 0부터 시작합니다. 
    • limit n offset m = m번째부터 시작해서 n개를 가져옵니다.
  • 결과 출력을 위한 2중 select문
    • subquery에 결과가 없을 때 아무것도 출력되지 않지만, 겉에 select를 해주면 null이 출력됩니다.
    • null을 출력하지 않아도 된다면, select subquery부분만 메인으로 사용하면 됩니다.

 

Code

 

select (select distinct salary 
        from employee
        order by 1 desc
        limit 1 offset 1
)as SecondHighestSalary

 

728x90

'Coding Test > SQL' 카테고리의 다른 글

[LeetCode] 1907. Count Salary Categories  (1) 2025.07.19
[LeetCode] 1934. Confirmation Rate  (0) 2025.07.13
[HackerRank] Top Earners  (0) 2025.06.14
[HackerRank] Placements  (1) 2025.06.14
[HackerRank] The Report  (0) 2025.06.14