select student,class,grade from (
select *
,row_number() over (partition by class order by grade) seq1
,row_number() over (partition by class order by grade desc) seq2
from tt ) t
where seq1=1 or seq2=1
结果如下:
select * from table where grade in(
select min(grade) from table group by class
union all
select max(grade) from table group by class)
order by class