# 如何写一个 SQL 获取分组 top 1 的数据
更多描述
一个学校中的每个学生属于一个班级,如何获取每个班的前三名
假设有学生表 student(id, score, class_id)
,score
代表分数,class_id
代表班级,如何写出 SQL 取每个班级分数前一名
与 Top n (opens new window) 类似,不过有更简单的方法
(当然考虑到并列情况就会复杂很多
Issue
欢迎在 Gtihub Issue 中回答此问题: Issue 8 (opens new window)
Author
单纯的获取 Top 1,只需要 group by
加聚合函数,不过复杂的还是参考 top n (opens new window)
select class_id, max(score) from student group by class_id