https://leetcode.com/problems/department-top-three-salaries/
Department Top Three Salaries - LeetCode
Can you solve this real interview question? Department Top Three Salaries - Table: Employee +--------------+---------+ | Column Name | Type | +--------------+---------+ | id | int | | name | varchar | | salary | int | | departmentId | int | +--------------
leetcode.com
185. Department Top Three Salaries
A company's executives are interested in seeing who earns the most money in each of the company's departments. A high earner in a department is an employee who has a salary in the top three unique salaries for that department.
Write an SQL query to find the employees who are high earners in each of the departments.
Return the result table in any order.
The query result format is in the following example.
회사의 경영진은 회사의 각 부서에서 가장 많은 돈을 버는 사람을 확인하는 데 관심이 있습니다. 부서의 고소득자는 해당 부서의 고유 급여 상위 3위 안에 드는 급여를 받는 직원입니다.
부서별 고소득 사원을 찾는 SQL 쿼리를 작성하세요.
정답
SELECT Department, Employee, Salary
FROM (
SELECT D.name Department
, E.name Employee
, E.salary Salary
, DENSE_RANK() OVER(PARTITION BY departmentid ORDER BY salary DESC) RNK
FROM Employee E JOIN Department D
ON E.departmentid = D.id) AS RNK_TABLE
WHERE RNK_TABLE.RNK <= 3;
DENSE_RANK 는 생략되는 순위없이 중복 순위도 함께 출력한다.
ex) 1,1,2,2,3,4...
2022.11.07 - [🛠️Skill/SQL] - [SQL 문법] RANK/DENSE_RANK, LAG, 분할 함수 / 테이블 분할
[SQL 문법] RANK/DENSE_RANK, LAG, 분할 함수 / 테이블 분할
1. 순위함수 SELECT 컬럼이름 , RANK() OVER (PARTITION BY 그룹 이름 ORDER BY 컬럼이름) FROM 테이블이름 1-1. RANK 중복 순위 포함해서 출력 ex) 1,1,3,4,5... 1-2. DENSE_RANK 중복 순위 무시하고 출력 (중간 순위를 비
dataanalysisdot.tistory.com
댓글