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:
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
University Class Enrollment AnalysisRaw DataStudent-ClassPairsEnrollment RecordsA โ†’ MathF โ†’ ComputerB โ†’ EnglishG โ†’ MathC โ†’ MathH โ†’ MathD โ†’ BiologyI โ†’ MathE โ†’ MathGroup ByClass NameGrouped ClassesMathA, C, EG, H, ICount: 6English: 1Biology: 1Computer: 1FilterCount โ‰ฅ 5Popular ClassesMathSQL: GROUP BY class HAVING COUNT(*) >= 5Efficient single-pass solution with O(n) time complexity๐ŸŽ“ Only Math qualifies as a popular class!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for k distinct classes in the result set

n
2n
โœ“ 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)
Asked in
Google 15 Amazon 25 Meta 12 Microsoft 18
89.5K Views
High Frequency
~15 min Avg. Time
2.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen