[MySQL] N번 째로 높은 값

테이블과 결과

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

문제

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

SELECT IFNULL(
    (
        SELECT DISTINCT salary 
        FROM Employee e 
        ORDER BY e.salary DESC
        LIMIT 1,1
    ), NULL
)  AS SecondHighestSalary
;

links

social