596 Classes More Than 5 Students 超過5名學(xué)生的課
Description:
There is a table courses with columns: student and class
Please list out all classes which have more than or equal to 5 students.
Example:
For example, the table:
+---------+------------+
| student | class |
+---------+------------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+------------+
Should output:
+---------+
| class |
+---------+
| Math |
+---------+
Note:
The students should not be counted duplicate in each course.
題目描述:
有一個(gè)courses 表 揩悄,有: student (學(xué)生) 和 class (課程)。
請(qǐng)列出所有超過或等于5名學(xué)生的課鬼悠。
示例 :
例如,表:
+---------+------------+
| student | class |
+---------+------------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+------------+
應(yīng)該輸出:
+---------+
| class |
+---------+
| Math |
+---------+
說明:
學(xué)生在每個(gè)課中不應(yīng)被重復(fù)計(jì)算删性。
思路:
使用 GROUP BY和 HAVING條件, 用 DISTINCT篩選不重復(fù)的學(xué)生
代碼:
MySQL:
SELECT
class
FROM
courses
GROUP BY
class
HAVING
COUNT ( DISTINCT student ) > 4