본문 바로가기
SQL

leetcode 185 department-top-three-salaries/description

by 무적김두칠 2023. 11. 16.

https://leetcode.com/problems/department-top-three-salaries/description/

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#Write your MySQL query statement below
with salary_rank as(
select id, name,  dense_rank() OVER (PARTITION BY departmentId ORDER BY salary DESC) AS 'rank'
from Employee
)
 
select 
d.name as 'Department'
, e.name as 'Employee'
, e.salary
from Employee e
left join Department d
on e.departmentId = d.id
left join salary_rank s
on s.id = e.id
where s.rank<=3
 
 
cs

rank 함수를 쓰시면 안돼구 dense_rank를 쓰셔야 되고
서브쿼리로 만든 후에 join걸으셔서 사용하시면 됩니다.

반응형

댓글