[176_MEDIUM] Second Highest Salary

2022. 9. 23. 16:57·SQL/leetcode

Table: Employee

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

 

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.

The query result format is in the following example.

 

Example 1:

Input: 
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
Output: 
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

Example 2:

Input: 
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
+----+--------+
Output: 
+---------------------+
| SecondHighestSalary |
+---------------------+
| null                |
+---------------------+

 

문제 조건 

두 번째로 높은 급여를 출력하시오

두 번째로 높은 급여가 없는 경우 null을 출력하시오 

 

문제 풀이

# limit 1 offset 1 (limit 1,1로 표현 가능) 

select ifnull((select distinct salary
			   from employee
               order by salary desc
               limit 1 offset 1),null) as SecondHighestSalary
저작자표시 (새창열림)

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

[178_MEDIUM] Rank Scores  (0) 2022.09.23
[177_MEDIUM] Nth Highest Salary  (0) 2022.09.23
[182_EASY] Duplicate Emails  (0) 2022.09.23
[181_EASY] Employees Earning More Than Their Managers  (0) 2022.09.23
[175_EASY] Combine Two Tables  (0) 2022.09.23
'SQL/leetcode' 카테고리의 다른 글
  • [178_MEDIUM] Rank Scores
  • [177_MEDIUM] Nth Highest Salary
  • [182_EASY] Duplicate Emails
  • [181_EASY] Employees Earning More Than Their Managers
소금깨
소금깨
  • 소금깨
    고군분투 인생살이
    소금깨
  • 전체
    오늘
    어제
    • 분류 전체보기 (325)
      • SQL (271)
        • programmers (27)
        • leetcode (198)
        • hacker rank (27)
        • solvesql (1)
        • 개념 (15)
      • etc (28)
        • 1) (13)
        • 2) (14)
      • error (6)
      • TABLEAU (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    프리미엄
    시계열데이터분석
    직장인자기계발
    Hard
    LeetCode
    HACKER_RANK
    easy
    solvesql
    패스트캠퍼스
    프로그래머스
    직장인인강
    SQL
    파이썬을활용한시계열데이터분석
    해커랭크
    group by
    패캠챌린지
    MySQL
    medium
    패스트캠퍼스후기
    파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
소금깨
[176_MEDIUM] Second Highest Salary
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.