본문 바로가기
LeetCode

[LeetCode] 34. Find First and Last Position of Element in Sorted Array

by 콩순이냉장고 2023. 9. 12.

문제 URL : https://leetcode.com/problems/primary-department-for-each-employee/

 

Primary Department for Each Employee - LeetCode

Can you solve this real interview question? Primary Department for Each Employee - Table: Employee +---------------+---------+ | Column Name | Type | +---------------+---------+ | employee_id | int | | department_id | int | | primary_flag | varchar | +----

leetcode.com

 

 

문제 풀이: union을 이용한 문제입니다.

union all 과 혼동하시면안됩니다.

 

union 과 union all 의 차이점은

union은 중복된 데이터를 걸러주지만

union all을 그렇지 않다는것

 

소스코드 :

# Write your MySQL query statement below
select employee_id, department_id
from employee
where primary_flag= 'Y'
union
select employee_id, department_id
from employee
group by employee_id
having count(employee_id) =1