https://leetcode.com/problems/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

- group by 사용해서 중복 제외

SELECT IFNULL((
    SELECT Salary AS SecondHighestSalary
    FROM Employee
    /* group by 미사용시 중복제거 안됨 */
    GROUP BY salary
    ORDER BY Salary DESC
    /* offset을 limit보다 먼저하면 에러 */
    LIMIT 1
    OFFSET 1),NULL)
AS SecondHighestSalary

- max 제외하고 distinct

select ifnull(
    (select distinct salary 
    from Employee
    where salary < (select max(salary) from Employee)
    order by salary desc
    limit 1), null)
as SecondHighestSalary;

+ Recent posts