- employee_id별 primary_flag='N'인 department_id 출력하기
=> department_id가 1개이면, 그대로 출력
=> department_id가 2개면,flag N 필터
UNION
사용
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
SUBQUERY
사용
SELECT DISTINCT employee_id, department_id
FROM Employee
WHERE employee_id IN (
SELECT employee_id
FROM Employee
GROUP BY employee_id
HAVING COUNT(*) = 1
)
OR primary_flag = 'Y'
ORDER BY employee_id;