반응형
Subquery를 사용하여 가장 높은 salary를 구해 department 단위로 조인을 했습니다.
그리고 department name을 출력하기 위해 department와 inner join을 하였습니다.
SELECT
d.name AS department
, e.name AS employee
, e.salary
FROM
employee AS e
INNER JOIN
(SELECT
departmentid, MAX(salary) AS max_salary
FROM
employee
GROUP BY
departmentid) dh
ON
e.departmentid = dh.departmentid
AND e.salary = dh.max_salary
INNER JOIN
department AS d
ON
d.id = e.departmentid
반응형
'Database' 카테고리의 다른 글
LeetCode Consecutive Numbers [ MySQL ] (0) | 2021.11.24 |
---|---|
HackerRank The Report [ MySQL ] (0) | 2021.11.24 |
LeetCode Delete Duplicate Emails [ MySQL ] (0) | 2021.11.01 |
LeetCode SQL Swap Salary [ MySQL ] (0) | 2021.11.01 |
[Mysql] Group By와 Having (0) | 2020.05.24 |