select
    a.department,
    concat (
        round(
            sum(
                if (
                    timestampdiff (second, b.first_clockin, b.last_clockin)/3600 > 9.5,
                    1,
                    0
                )
            ) / count(a.department) * 100,
            1
        ),
        '%'
    ) as ratio
from
    staff_tb a
    join attendent_tb b on a.staff_id = b.staff_id
group by
    a.department
order by
    ratio desc