Skip to content

Latest commit

 

History

History
38 lines (31 loc) · 826 Bytes

09-Classes-More-Than-5-Students.md

File metadata and controls

38 lines (31 loc) · 826 Bytes

Classes More Than 5 Students

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.

一种办法是子查询,第二种使用 Having 更加简洁。

SELECT class FROM courses GROUP BY class HAVING COUNT(distinct student) >= 5