Skip to content

1789. Primary Department for Each Employee

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
SELECT
  employee_id,
  department_id
FROM Employee
WHERE primary_flag = 'Y'
UNION DISTINCT
SELECT
  employee_id,
  department_id
FROM Employee
GROUP BY 1
HAVING COUNT(*) = 1;