관리 메뉴

오늘도 배운다

177. Nth Highest Salary / LeetCode, SQL, MySQL 본문

코딩테스트연습(SQL)

177. Nth Highest Salary / LeetCode, SQL, MySQL

LearnerToRunner 2023. 3. 5. 10:03

문제

source: LeetCode

Write an SQL query to report the nth highest salary from the Employee table. If there is no nth highest salary, the query should report null.

The query result format is in the following example.

 

 

 

 

제출답안(MySQL)

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
      SELECT DISTINCT(salary)
      FROM
        (SELECT id, salary, DENSE_RANK() OVER(ORDER BY salary DESC) AS ranking
        FROM employee) AS emp_with_ranking
    WHERE ranking = N
  );
END

 

 

 

문제 바로가기(MySQL)

728x90
Comments