2023-01-03:超过5名学生的课。编写一个SQL查询来报告 至少有5个学生 的所有班级,返回结果不限顺序。请问sql语句如何写?
±--------+
| class |
±--------+
| Math |
±--------+
DROP TABLE IF EXISTS `courses`
CREATE TABLE `courses` (
`student` varchar(255) NOT NULL,
`class` varchar(255) NOT NULL,
UNIQUE KEY `student` (`student`,`class`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `courses` VALUES ('A', 'Math');
INSERT INTO `courses` VALUES ('B', 'English');
INSERT INTO `courses` VALUES ('C', 'Math');
INSERT INTO `courses` VALUES ('D', 'Biology');
INSERT INTO `courses` VALUES ('E', 'Math');
INSERT INTO `courses` VALUES ('F', 'Computer');
INSERT INTO `courses` VALUES ('G', 'Math');
INSERT INTO `courses` VALUES ('H', 'Math');
INSERT INTO `courses` VALUES ('I', 'Math');
答案2023-01-03:
sql语句如下:
SELECT class FROM courses GROUP BY class HAVING COUNT(DISTINCT student) >= 5