思路1:
即按课程分组,由于有重复数据,按学生计数时需要使用distinct.
SELECT CLASS class
FROM COURSES
GROUP BY CLASS
HAVING COUNT(DISTINCT STUDENT) >= 5
分析1:
此代码是我最初的思路,也是LeetCode记录的最优解。但是在最近增加了一个极大的测试用例导致超时。
思路2:
二次选择。是第一种思路的一种改写。
代码2:
select class
from (
select class,count(distinct student) as num
from courses
group by class
) as c
where num>=5;