- 超过5名学生的课
有一个courses 表 ,有: student (学生) 和 class (课程)。
请列出所有超过或等于5名学生的课。
例如,表:
student | class |
---|---|
A | Math |
B | English |
C | Math |
D | Biology |
E | Math |
F | Computer |
G | Math |
H | Math |
I | Math |
应该输出:
class |
---|
Math |
题目来源
https://leetcode-cn.com/problems/classes-more-than-5-students/
思路
1. 先按照class进行分组
2.分组后筛选,使用having
3.筛选条件采用对distinct
name进行统计
题解
select class
from courses
group by class
having count(distinct student)>=5;