[1965_EASY] Employees With Missing Information

2022. 8. 7. 19:16·SQL/LeetCode
 

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

 

저작자표시 (새창열림)

'SQL > LeetCode' 카테고리의 다른 글

[1355#_MEDIUM]. Activity Participants  (0) 2022.08.08
[1978#_EASY] Employees Whose Manager Left the Company  (0) 2022.08.07
[1939#_EASY] Users That Actively Request Confirmation Messages  (0) 2022.08.07
[1341#_MEDIUM] Movie Rating  (0) 2022.08.05
[1321#_MEDIUM] Restaurant Growth  (0) 2022.08.05
'SQL/LeetCode' 카테고리의 다른 글
  • [1355#_MEDIUM]. Activity Participants
  • [1978#_EASY] Employees Whose Manager Left the Company
  • [1939#_EASY] Users That Actively Request Confirmation Messages
  • [1341#_MEDIUM] Movie Rating
소금깨
소금깨
  • 소금깨
    고군분투 인생살이
    소금깨
  • 전체
    오늘
    어제
    • 분류 전체보기 (328)
      • SQL (271)
        • 프로그래머스 (27)
        • LeetCode (198)
        • Hacker Rank (27)
        • Solve SQL (1)
        • 개념 (15)
      • 데이터 분석 (16)
        • 참고하며 공부하기 (14)
      • 기타 (15)
        • 통계 (14)
      • 오류 (6)
      • 인생살이 (0)
        • 리뷰 (0)
        • 일기 (0)
      • 中文 (0)
      • TABLEAU (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    시계열데이터분석
    파이썬을활용한시계열데이터분석
    직장인자기계발
    solvesql
    프로그래머스
    MySQL
    프리미엄
    파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
    패스트캠퍼스후기
    패스트캠퍼스
    HACKER_RANK
    group by
    패캠챌린지
    SQL
    medium
    해커랭크
    직장인인강
    easy
    LeetCode
    Hard
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
소금깨
[1965_EASY] Employees With Missing Information
상단으로

티스토리툴바