Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- RANK
- recursive
- 패스트캠퍼스
- 시계열데이터분석
- row_number
- MySQL
- SELF-JOIN
- 다시풀어보기
- 파이썬을활용한시계열데이터분석
- 패캠챌린지
- join
- 해커랭크
- easy
- 직장인인강
- 프로그래머스
- solvesql
- 직장인자기계발
- meidum
- 어려웠음
- Hackerrank
- LeetCode
- HACKER_RANK
- group by
- medium
- Hard
- SQL
- 패스트캠퍼스후기
- lv.4
- 프리미엄
- 파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
Archives
- Today
- Total
~고군분투 인생살이~
[184_MEDIUM] Department Highest Salary 다시풀기 본문
Table: Employee
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| id | int |
| name | varchar |
| salary | int |
| departmentId | int |
+--------------+---------+
id is the primary key column for this table.
departmentId is a foreign key of the ID from the Department table.
Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.
Table: Department
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
id is the primary key column for this table.
Each row of this table indicates the ID of a department and its name.
Write an SQL query to find employees who have the highest salary in each of the departments.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input:
Employee table:
+----+-------+--------+--------------+
| id | name | salary | departmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Jim | 90000 | 1 |
| 3 | Henry | 80000 | 2 |
| 4 | Sam | 60000 | 2 |
| 5 | Max | 90000 | 1 |
+----+-------+--------+--------------+
Department table:
+----+-------+
| id | name |
+----+-------+
| 1 | IT |
| 2 | Sales |
+----+-------+
Output:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Jim | 90000 |
| Sales | Henry | 80000 |
| IT | Max | 90000 |
+------------+----------+--------+
Explanation: Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
문제 조건
해당 부서에서 salary를 가장 많이 받는 사람을 출력하시오
쿼리
with temp as(
select e.name as Employee,
e.salary as Salary,
d.name as Department,
rank() over(partition by departmentId order by salary desc) as rnk
from employee e
join department d on e.departmentId = d.id)
select Department,
Employee,
Salary
from temp
where rnk = 1
'SQL > LEETCODE' 카테고리의 다른 글
[511_EASY] Game Play Analysis I (1) | 2022.09.30 |
---|---|
[608_MEDIUM] Tree Node 다시풀기 (0) | 2022.09.26 |
[180_MEDIUM] Consecutive Numbers 다시풀기 (0) | 2022.09.26 |
[197_EASY] Rising Temperature (0) | 2022.09.25 |
[183_EASY] Customers Who Never Order (0) | 2022.09.25 |