184. Department Highest Salary
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
-- 1. 출력: department name, employee name, salary
-- 2. 필터: the highest salary in each of the departments
-- 3. 정렬: X
WITH max AS(
SELECT e.name AS Employee
,e.salary AS salary
,d.name AS Department
,MAX(e.salary) OVER (PARTITION BY d.name) AS dh_salary
FROM employee e
LEFT JOIN department d ON e.departmentId = d.id
)
SELECT Department
,Employee
,Salary
FROM max
WHERE Salary = dh_salary
WITH문과 윈도우 함수를 사용하여 풀이
employee 테이블과 department 테이블에 이름이 같은 컬럼이 있어서 필요한 변수만 WITH문에서 출력
여러가지 방법으로 풀어봤는데 가장 간단한 것 같은 풀이로 포스팅 해본다 :)
'SQL > LeetCode' 카테고리의 다른 글
| [SQL] LeetCode: 180. Consecutive Numbers (0) | 2024.02.08 |
|---|---|
| [SQL] LeetCode: 610. Triangle Judgement (0) | 2024.02.08 |
| [SQL] LeetCode: 1789. Primary Department for Each Employee (1) | 2024.01.26 |
| [SQL] LeetCode: 1731. The Number of Employees Which Report to Each Employee (0) | 2024.01.17 |
| [SQL] LeetCode: 177. Nth Highest Salary (0) | 2023.06.28 |