The following employee table shows which department(s) an employee is in and which one is their primary department. If an employee only belongs to one department their primary flag will show 'N' (even though that is their primary department as it's their only one) and if they have multiple departments it will flag 'Y' or 'N' to show which one is primary.
Employee:
| employee_id | department_id | primary_flag |
|---|---|---|
| 1 | 1 | N |
| 2 | 1 | Y |
| 2 | 2 | N |
| 3 | 3 | N |
| 4 | 2 | N |
| 4 | 3 | Y |
| 4 | 4 | N |
Expected output--the primary department for employee_id is department_id
| employee_id | department_id |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 3 | 3 |
| 4 | 3 |
My solution returns nothing.
Why does it not work?
There are no null values and when I run the subquery by itself it shows the employee_id, department_ids that should not be in the final output.
select employee_id, department_id
from Employee
WHERE (employee_id, department_id) not in(
select employee_id, department_id
from (
SELECT employee_id, department_id, primary_flag,
count(employee_id) over (partition by employee_id) as cnt
from employee
) a
WHERE cnt > 1 and primary_flag = 'n'
);
From Joel Coehoorn's comments below, he was able to produce the same results; however it does not work for me in LeetCode.

I combined Employee and Department together
Fiddle