有一个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 | +---------+
Note:
学生在每个课中不应被重复计算。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/classes-more-than-5-students
解题思路:超过或等于5名学生的课,可使用courses表中的class进行分组,再通过分组进行计数,利用分组条件 having 子句进行判定哪个课选的人超过或等于5个(使用count()函数)。根据其提示学生在每个课中不应该被重复计算,则应该填上 distinct 关键字去掉重复记录。
SQL语句:
select class from courses group by class having count(distinct student)>=5