问题596:有一个courses
表 ,有student
(学生)和class
(课程)。请列出所有超过或等于5
名学生的课。学生在每个课中不应被重复计算。例如,表:
应该输出:
解法一:
SELECT DISTINCT tab1.class
FROM courses AS tab1
WHERE (SELECT COUNT(DISTINCT tab2.student)
FROM courses AS tab2
WHERE tab2.class = tab1.class) >= 5
运行结果:
解法二:
SELECT DISTINCT tab1.class
FROM courses AS tab1
WHERE (SELECT COUNT(tab2.student)
FROM (SELECT DISTINCT * FROM courses) AS tab2
WHERE tab2.class = tab1.class) >= 5
运行结果: