inblog logo
|
silver
    SQL문제풀기

    [SQL문제풀기] Second Highest Salary

    silver's avatar
    silver
    Nov 21, 2025
    [SQL문제풀기] Second Highest Salary
    Contents
    문제MySQL

    문제

    Second Highest Salary - LeetCode
    Can you solve this real interview question? Second Highest Salary - 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.   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.   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 | +---------------------+
    Second Highest Salary - LeetCode
    https://leetcode.com/problems/second-highest-salary/?envType=study-plan-v2&envId=top-sql-50
    Second Highest Salary - LeetCode

    MySQL

    내가 작성한 오답

    -- 1 select salary SecondHighestSalary from (select distinct salary from Employee order by salary limit 1 offset 1) a;
    notion image
    -- 2 -- DENSE_RANK() 때문에 s = 2인 행이 여러 개 생겨서, 서브쿼리가 값 1개가 아니라 여러 행을 리턴 select (select salary from(select dense_rank() over(order by salary desc) s, salary from Employee) a where s = 2) SecondHighestSalary;

    내가 작성한 정답

    -- 1 select (select distinct salary from Employee order by salary desc limit 1 offset 1) SecondHighestSalary
    -- 2 select (select distinct salary from(select dense_rank() over(order by salary desc) s, salary from Employee) a where s = 2) SecondHighestSalary;
    -- 3 select max(salary) SecondHighestSalary from Employee where salary < (select max(salary) from Employee);
    Share article

    silver

    RSS·Powered by Inblog