250x250
Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
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
Archives
Today
Total
관리 메뉴

데이터 놀이터

Leetcode - 184. Department Highest Salary 본문

SQL

Leetcode - 184. Department Highest Salary

jjjk84 2023. 9. 23. 21:28
728x90

1. 문제

 

Table: Employee

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| id           | int     |
| name         | varchar |
| salary       | int     |
| departmentId | int     |
+--------------+---------+
id is the primary key (column with unique values) for this table.
departmentId is a foreign key (reference columns) 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 with unique values) for this table. It is guaranteed that department name is not NULL.
Each row of this table indicates the ID of a department and its name.

 

Write a solution to find employees who have the highest salary in each of the departments.

Return the result table in any order.

The result format is in the following example.

 

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.

https://leetcode.com/problems/department-highest-salary/

 

2. 문제 조건

 

  1. find employees who have the highest salary in each of the departments. -> 부서별로 GROUP BY, MAX를 통해 hightest salary 찾기

 

3. 코드 작성

 

SELECT d.name AS Department
     , e.name AS Employee
     , m.max_salary AS Salary
FROM employee AS e
    JOIN (SELECT departmentId
        , MAX(salary) AS max_salary
        FROM employee
        GROUP BY departmentId) AS m ON e.departmentId = m.departmentId
                                        AND e.salary = m.max_salary
    JOIN department as d ON e.departmentId = d.id;

 

4. 회고

 

  • 서브쿼리를 활용하여 JOIN을 할 수 있다는 사실을 알게 되었다.
    • 서브쿼리로 원하는 테이블을 만들어, 기존 테이블과 JOIN을 통해 문제에서 요구하는 데이터만 추출할 수 있다.
728x90

'SQL' 카테고리의 다른 글

Hackerrank - The Report  (0) 2023.09.24
Hackerrank - challenges  (0) 2023.09.24
SQL 고급 강의 - ERD  (0) 2023.09.22
SQL 고급 강의 - DML  (0) 2023.09.21
Hackerrank - New Companies  (0) 2023.09.10