with A as ( select staff_id, case when timestampdiff(minute,first_clockin,last_clockin)>570 then 1 else 0 END as oot from attendent_tb ) select department, concat(round(sum(oot)*100/count(*),1),'%') as ratio from A join staff_tb st on A.staff_id=st.staff_id group by department order by ratio desc