일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- hackerrank
- sql
- coding
- time complexity
- Growth hacking
- 프로그래머스
- mysql
- lambda
- import re
- LeetCode
- itertools
- Stack
- Level2
- Queue
- 코테
- join
- 완전탐색
- lv4
- cte
- AARRR
- 코딩
- coding test
- 파이썬
- 조합
- 코딩테스트
- counter
- programmers
- python
- level4
- tableau
Archives
- Today
- Total
ror_coding
[LeetCode] 176. Second Highest Salary 본문
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 |