描述
有一个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 |
+---------+
解题
select
a.class
from
(select DISTINCT * from courses) a
group by
a.class
having
count(a.student) >= 5;
但是看到一个更好的方法
SELECT
class
FROM
courses
GROUP BY
class
HAVING
COUNT(DISTINCT student) >= 5;