Classes With at Least 5 Students - Problem
Problem Statement:
You are given a database table containing student enrollment records. Your task is to identify popular classes that have attracted at least 5 students.
Table Structure:
The combination
Goal: Write a SQL query to find all classes that have at least 5 students enrolled. Return the result in any order.
Example:
Input table:
Output:
You are given a database table containing student enrollment records. Your task is to identify popular classes that have attracted at least 5 students.
Table Structure:
Courses table:+-------------+---------+
| Column Name | Type |
+-------------+---------+
| student | varchar |
| class | varchar |
+-------------+---------+
The combination
(student, class) is the primary key, ensuring each student-class pair is unique.Goal: Write a SQL query to find all classes that have at least 5 students enrolled. Return the result in any order.
Example:
Input table:
+---------+----------+
| student | class |
+---------+----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+----------+
Output:
+---------+
| class |
+---------+
| Math |
+---------+
Input & Output
example_1.sql โ Basic Example
$
Input:
Courses table:
+---------+----------+
| student | class |
+---------+----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+----------+
โบ
Output:
+---------+
| class |
+---------+
| Math |
+---------+
๐ก Note:
Math has 6 students (A, C, E, G, H, I) which is >= 5. English has 1, Biology has 1, and Computer has 1 student each, so they don't meet the threshold.
example_2.sql โ Multiple Popular Classes
$
Input:
Courses table:
+---------+----------+
| student | class |
+---------+----------+
| A | Math |
| B | English |
| C | Math |
| D | English |
| E | Math |
| F | English |
| G | Math |
| H | English |
| I | Math |
| J | English |
+---------+----------+
โบ
Output:
+---------+
| class |
+---------+
| Math |
| English |
+---------+
๐ก Note:
Both Math and English have exactly 5 students each, so both classes are returned.
example_3.sql โ No Popular Classes
$
Input:
Courses table:
+---------+---------+
| student | class |
+---------+---------+
| A | Math |
| B | English |
| C | Biology |
| D | Physics |
+---------+---------+
โบ
Output:
+---------+
| class |
+---------+
(empty result)
๐ก Note:
Each class has only 1 student, so no class meets the minimum requirement of 5 students.
Visualization
Tap to expand
Understanding the Visualization
1
Collect Enrollment Data
Gather all student-class enrollment pairs from the registration system
2
Group by Class
Organize the data by grouping all students who enrolled in the same class together
3
Count Students per Class
Count how many students are in each class group
4
Apply Popularity Threshold
Filter to keep only classes that meet the minimum enrollment requirement (โฅ5 students)
Key Takeaway
๐ฏ Key Insight: GROUP BY with HAVING is the perfect SQL pattern for counting occurrences and filtering based on counts - it's efficient, readable, and leverages database engine optimizations.
Time & Space Complexity
Time Complexity
O(n)
Single pass through the table to group and count
โ Linear Growth
Space Complexity
O(k)
Space for k distinct classes in the result set
โ Linear Space
Constraints
- 1 โค Number of rows โค 104
- 1 โค student.length โค 20
- 1 โค class.length โค 20
- student and class consist of letters and numbers only
- Each (student, class) pair is unique (primary key constraint)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code