select * from(SELECT count(*) as count FROM `medal` `m` INNER JOIN `medal_list` `ml` ON `ml`.`medalId`=`m`.`Id` GROUP BY ml.stuId) table_a order by table_a.count desc