LeetCode: Classes More Than Students

LeetCode: Classes More Than 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:

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

Should output:

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

Note:

The students should not be counted duplicate in each course.

1
2
# Write your MySQL query statement below
select class from (select distinct student, class from courses) t group by class having count(*) >=5;