select *
from (
select 
    tag as tid,
    uid,
    row_number() over (partition by tag order by max_score desc,min_score desc,uid desc) as ranking
from
(select 
    tag,
    uid,
    max(score) as max_score,
    min(score) as min_score
from exam_record join examination_info using(exam_id)
group by tag,uid) rnk) rnk_a
where ranking <=3