728x90
select b.Name as Department , a.Name as Employee, a.Salary
from (select *, dense_rank() over (partition by DepartmentId order by salary desc) as sal_rank
from Employee ) a
join Department b
on a.DepartmentId = b.Id
where a.sal_rank <= 3
order by a.id ;
SELECT Name as 'Department', Employee, Salary
FROM (
SELECT Salary, Name as 'Employee', DepartmentId, DENSE_RANK() OVER(PARTITION BY DepartmentId ORDER BY Salary DESC) as salaryRank
FROM Employee) as withSalaryRank
LEFT JOIN Department
ON withSalaryRank.DepartmentId = Department.Id
WHERE salaryRank < 4
AND Name IS NOT NULL
728x90
'SQL' 카테고리의 다른 글
[LeetCode] Trips and Users (date between) (0) | 2021.04.10 |
---|---|
[LeetCode] Department Highest Salary ( sub query, partition by) (0) | 2021.04.10 |
SQL 실행 순서와 문법 순서 (0) | 2021.04.10 |
[LeetCode] Consecutive Numbers (self join twice) (0) | 2021.04.10 |
[LeetCode] Rank Scores (rank, dense rank, row number) (0) | 2021.04.10 |
댓글