596. Classes More Than 5 Students
Problem Statement
Find all classes that have at least five students. Return the result table in any order.
Table Schema
Table: Courses
+-------------+---------+
| student | varchar |
| class | varchar |
+-------------+---------+
(student, class) is the primary key.
Examples
Example 1
Input Table:
Courses table:
+---------+----------+
| student | class |
+---------+----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+----------+
Expected Output:
+-------+
| class |
+-------+
| Math |
+-------+
SQL Solution
SELECT class
FROM Courses
GROUP BY class
HAVING COUNT(DISTINCT student) >= 5;
Problem Info
DifficultyEASY
Topics
group-byhavingdistinct
Reference Links