You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Write your MySQL query statement belowselect employee_id, department_id from Employee group by employee_id havingcount(department_id)=1unionselect employee_id, department_id from Employee
where primary_flag='Y'
Solution v2: count() over(partition by)
selectt.employee_id, t.department_idfrom
(select employee_id, department_id, primary_flag, count(department_id) over(partition by employee_id) as cnt from Employee) as t
wheret.cnt=1ort.primary_flag='Y'
count() is what you want to do
over(partition by) is where to do, the range or the field of count()
the overall meaning is: perform count(department_id) function to every employee_id
comparing to group by: it keeps every line. group by will combine every line