LeetCode solutions

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

Read the problem on LeetCode View on GitHub

SQL

Accepted on LeetCode — runtime 513ms, memory 0.0B, accepted 2025-12-31.

sql
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

Source