select
    *
from
    (
        select
            tag as tid,
            uid,
            rank() over (
                partition by
                    tag
                order by
                    max(score) desc,
                    min(score) desc,
                    uid desc
            ) as ranking
            
        from
            exam_record
            join examination_info using (exam_id)
        group by
            tag,
            uid
    ) t
where
    t.ranking <= 3