1942. Primary Department for Each Employee
My accepted SQL solution to LeetCode problem 1942, Primary Department for Each Employee, running in 513ms.
- Difficulty: Easy
- SQL
- Runtime 513ms
- Memory 0.0B
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
SQL
Accepted on LeetCode — runtime 513ms, memory 0.0B, accepted 2025-12-31.
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