为有牺牲多壮志,敢教日月换新天。

[SQL]LeetCode596. 超过5名学生的课 | Classes More Than 5 Students

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10450346.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

SQL架构

 1 Create table If Not Exists courses (student varchar(255), class varchar(255))
 2 Truncate table courses
 3 insert into courses (student, class) values ('A', 'Math')
 4 insert into courses (student, class) values ('B', 'English')
 5 insert into courses (student, class) values ('C', 'Math')
 6 insert into courses (student, class) values ('D', 'Biology')
 7 insert into courses (student, class) values ('E', 'Math')
 8 insert into courses (student, class) values ('F', 'Computer')
 9 insert into courses (student, class) values ('G', 'Math')
10 insert into courses (student, class) values ('H', 'Math')
11 insert into courses (student, class) values ('I', 'Math')

There is a table courses with columns: student and class

Please list out all classes which have more than or equal to 5 students.

For example, the table:

+---------+------------+
| student | class      |
+---------+------------+
| A       | Math       |
| B       | English    |
| C       | Math       |
| D       | Biology    |
| E       | Math       |
| F       | Computer   |
| G       | Math       |
| H       | Math       |
| I       | Math       |
+---------+------------+

Should output:

+---------+
| class   |
+---------+
| Math    |
+---------+ 

Note:
The students should not be counted duplicate in each course.


有一个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    |
+---------+

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


1465ms

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

1479ms

1 # Write your MySQL query statement below
2 SELECT 
3     class
4 FROM
5     courses
6 GROUP BY class
7 HAVING COUNT(DISTINCT student)>=5

 

posted @ 2019-02-28 14:48  为敢技术  阅读(276)  评论(0编辑  收藏  举报