select id, job, score, t_rank from ( select id, job, score, count(*) over(partition by job) as cnt, row_number() over(partition by job order by score desc) as t_rank from grade ) a where t_rank = FLOOR((cnt + 1) / 2) or t_rank = CEIL((cnt + 1) / 2) order by id ;