LeetCode #596 Classes More Than 5 Students 超过5名学生的课

596 Classes More Than 5 Students 超过5名学生的课

Description:
There is a table courses with columns: student and class

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

Example:

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    |
+---------+

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

思路:

使用 GROUP BY和 HAVING条件, 用 DISTINCT筛选不重复的学生

代码:
MySQL:

SELECT
    class
FROM
    courses
GROUP BY
    class
HAVING
    COUNT ( DISTINCT student ) > 4
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,753评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,939评论 0 23
  • There is a table courses with columns: student and class ...
    无敌的肉包阅读 307评论 0 0
  • 立业先修身 成才先成人 尽人事 听天命 谋事在人 成事在天 仁 义 礼 智 信 交朋友之根本 仁者,易也。凡事不能...
    music四四阅读 389评论 0 0
  • 看日记 孩子 二月份有三十号了 晴天你也不见太阳 金鱼也会被水淹死 太让人伤心了 (有些孩子该长点心了,否则太令人...
    旖旎i阅读 489评论 2 15

友情链接更多精彩内容