본문 바로가기

SQL

(SQL) - LeetCode (easy) 1789. Primary Department for Each Employee

반응형

https://leetcode.com/problems/primary-department-for-each-employee/description/

group by와 where절을 사용해본 문제였습니다.

📕 풀이방법

📔 풀이과정

primary_flag = 'Y'거나 속한 부서가 1개인 employee_id, department_id를 선택해줍니다.


📕 Code

📔 ANSI SQL

SELECT employee_id, department_id
FROM employee e
WHERE primary_flag = 'Y'
OR (
  employee_id in (
    select employee_id from employee
    group by employee_id 
    having count(employee_id) = 1
  )
)

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.