leetcode-627-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:

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

分析


Group BY Having 语法

解决方案(MySQL)


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

题目来源