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

你可能感兴趣的:(LeetCode #596 Classes More Than 5 Students 超过5名学生的课)