LeetCode每天一题

596. 超过5名学生的课

596. 超过5名学生的课

难度简单151

SQL架构

有一个courses 表 ,有: student (学生) 和 **class (课程)**。

请列出所有超过或等于5名学生的课。

例如,表:

1
2
3
4
5
6
7
8
9
10
11
12
13
+---------+------------+
| student | class |
+---------+------------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+------------+

应该输出:

1
2
3
4
5
+---------+
| class |
+---------+
| Math |
+---------+

Note:
学生在每个课中不应被重复计算。

通过次数39,998

提交次数97,884

解题思路

这个可以使用having

1
2
3
4
select class
from courses
group by class
having count(DISTINCT student)>= 5;

答案解析

1
2
3
4
5
# Write your MySQL query statement below
select class
from courses
group by class
having count(DISTINCT student)>= 5;

image-20200723221622326

点击查看
-------------------本文结束 感谢您的阅读-------------------