(select grade
from (select grade,sum(number)over(order by grade) s1,
sum(number)over() / 2 m
from class_grade order by grade) a
where a.s1 >= a.m
limit 1)
union
(select grade
from (select grade,sum(number)over(order by grade desc) s2,
sum(number)over() / 2 m
from class_grade order by grade desc) b
where b.s2 >= b.m
limit 1);