SQL/LeetCode
[1965_EASY] Employees With Missing Information
소금깨
2022. 8. 7. 19:16
Employees With Missing Information - LeetCode
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
Table: Employees
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| name | varchar |
+-------------+---------+
employee_id is the primary key 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 primary key for this table.
Each row of this table indicates the salary of the employee whose ID is employee_id.
Write an SQL query 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 query 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.
문제 조건
- 직원의 이름 누락 혹은 급여가 누락된 직원의 ID를 출력하시오
- employee_id를 기준으로 오름차순 하시오
문제 풀이
- name이 누락된 테이블, salary가 누락 된 테이블을 생성
- left join을 이용해서 employees와 salary 테이블을 연결시키고 각 null값이 있는 employee_id를 출력
- 그 다음 누락된 정보가 담긴 두 테이블을 union으로 합쳐줌
select e.employee_id
from employees e
left join salaries s on e.employee_id = s.employee_id
where s.salary is null
union
select s.employee_id
from salaries s
left join employees e on s.employee_id = e.employee_id
where e.name is null
order by 1