```select
*
from
(
select
tag,uid,
row_number()over(partition by tag order by dd desc, xx desc,uid desc ) ranking
from
(
select
tag,uid,
max(score) dd, --
min(score) xx --
from exam_record
join examination_info
on exam_record.exam_id=examination_info.exam_id
group by tag,uid
) t1 -- ①求出每个人在每类试卷中的最大最小值
)t2 -- ②按照要求进行排序
where ranking<=3 -- ③去前三