[LeetCode][MySQL] 1965. Employees With Missing Information
Easy 🔗1965. Employees With Missing Information
📝문제 요약
Table: Employees
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| name | varchar |
+-------------+---------+
employee_id is the column with unique values for this table.
Each row of this table indicates the name of the employee whose ID is employee_id.
Table: Salaries
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| salary | int |
+-------------+---------+
employee_id is the column with unique values for this table.
Each row of this table indicates the salary of the employee whose ID is employee_id.
Write a solution to report the IDs of all the employees with missing information. The information of an employee is missing if:
- The employee’s name is missing, or
- The employee’s salary is missing.
Return the result table ordered by employee_id
in ascending order.
The result format is in the following example.
Example 1:
Input:
Employees table:
+-------------+----------+
| employee_id | name |
+-------------+----------+
| 2 | Crew |
| 4 | Haven |
| 5 | Kristian |
+-------------+----------+
Salaries table:
+-------------+--------+
| employee_id | salary |
+-------------+--------+
| 5 | 76071 |
| 1 | 22517 |
| 4 | 63539 |
+-------------+--------+
Output:
+-------------+
| employee_id |
+-------------+
| 1 |
| 2 |
+-------------+
Explanation:
Employees 1, 2, 4, and 5 are working at this company.
The name of employee 1 is missing.
The salary of employee 2 is missing.
✏️문제 풀이
첫 번째 쿼리
SELECT e.employee_id
FROM Employees e LEFT JOIN Salaries s ON e.employee_id = s.employee_id
WHERE s.employee_id IS NULL
Employees
테이블을 기준으로LEFT JOIN
을 수행- 즉, 모든 직원 정보(
Employees
)를 다 가져오고,Salaries
에 해당 직원의 급여가 있으면 붙여줌 Salaries
에 정보가 없는 직원은s.employee_id
가NULL
이 됨
UNION
- 두 쿼리 결과를 합침(중복 자동 제거)
두 번째 쿼리
SELECT s.employee_id
FROM Salaries s LEFT JOIN Employees e ON s.employee_id = e.employee_id
WHERE e.employee_id IS NULL
Salaries
테이블을 기준으로LEFT JOIN
- 급여 정보는 있는데 직원 정보가 없는 경우(
e.employee_id IS NULL
)를 찾음
ORDER BY
ORDER BY employee_id
employee_id
기준으로 오름차순 정렬
💯제출 코드
SELECT e.employee_id
FROM Employees e LEFT JOIN Salaries s ON e.employee_id = s.employee_id
WHERE s.employee_id IS NULL
UNION
SELECT s.employee_id
FROM Salaries s LEFT JOIN Employees e ON s.employee_id = e.employee_id
WHERE e.employee_id IS NULL
ORDER BY employee_id
댓글남기기