Classes With at Least 5 Students solution and explanation
9 more videos on this problem
Classes More Than 5 Students | Leetcode 596 | Crack SQL Interviews in 50 Qs #mysql #leetcodeLearn With Chirag · 4:38 · 6K views
LeetCode 596: Classes More Than 5 Students [SQL]Frederik Müller · 8:12 · 6K views
596. Classes More Than 5 Students | SQL | LeetCode |SheCodes · 3:29 · 118 views
LeetCode 596: Classes With at Least 5 Students | SQL Solution ExplainedSimplify Data with Isha · 4:00 · 101 views
596. Classes More Than 5 Students | LeetCode | SQL | Solution | Interview ProblemData Alchemist · 2:14 · 97 views
Classes With at Least 5 Students | Leetcode 596Code with Siddhant · 2:44 · 82 views
Leetcode - 596 | Classes With at Least 5 Students | SQL Series | Easy ProblemBugToBuild · 2:34 · 72 views
Find Classes with At Least 5 Students | SQL Interview Question | LeetCode 596 | GeekyCodesGeeky Codes · 2:39 · 54 views
LeetCode 596 | Classes With At Least 5 Students | SQL Study Plan | Top 50 SQLAlgoChat · 5:21 · 33 views
Leetcode 596 Classes With at Least 5 Students - SQL Interview Question - Explained In HindiAlgorithmicIQ · 6:13 · 30 views
The problem
Table: Courses
+-------------+---------+ | Column Name | Type | +-------------+---------+ | student | varchar | | class | varchar | +-------------+---------+ (student, class) is the primary key (combination of columns with unique values) for this table. Each row of this table indicates the name of a student and the class in which they are enrolled.
Write a solution to find all the classes that have at least five students.
Return the result table in any order.
The result format is in the following example.
Example 1:
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 | +---------+ Explanation: - Math has 6 students, so we include it. - English has 1 student, so we do not include it. - Biology has 1 student, so we do not include it. - Computer has 1 student, so we do not include it.
Approach overview
Problem Overview: You have a Courses table with one row per student per class. Return every class that has at least 5 students enrolled. Each class should appear once in the output.
Approach 1: Derived Table Filter (O(n) time, O(k) space)
Run an inner query that groups rows by class and computes COUNT(DISTINCT student) for each group. Wrap that result in a derived table, then filter with WHERE cnt >= 5 in the outer query. The database scans the table once to build the groups, then scans the k groups once more to apply the filter. This version makes the two-step logic explicit, which helps when you need the count for other columns or later joins. Use it when you want to reason about each stage separately.
Approach 2: GROUP BY with HAVING (O(n) time, O(k) space)
Group by class and put the condition directly in HAVING COUNT(DISTINCT student) >= 5. HAVING runs after aggregation, so it is the correct place to filter on an aggregate. The query is one pass over the table with a hash table keyed by class, where k is the number of distinct classes. There is no extra subquery to write or read. This is the version you should write by default. Most MySQL optimizers produce the same plan for both approaches, so pick the one that reads more clearly to you.
Why DISTINCT matters: The table key is (student, class), so a student cannot enroll in the same class twice. COUNT(*) would give the same result here. COUNT(DISTINCT student) is still the safer habit because it stays correct if the schema changes or duplicate rows appear.
Recommended for interviews: Interviewers expect the GROUP BY and HAVING solution. Writing a correlated subquery that counts rows for each class shows you understand the problem, but it runs the count once per class and is slower on large tables. Using GROUP BY with HAVING and explaining why HAVING is needed instead of WHERE shows the skill they are testing. See more on database problems and aggregation techniques on FleetCode.
Solution
Thinking
Classes with at least five students. Group-count and filter with
HAVING; a subquery is unnecessary.
GROUP BY classthenHAVING COUNT(*) >= 5.HAVINGruns after grouping, unlikeWHERE.
We can use the GROUP BY statement to group by class and then use the HAVING statement to filter out the classes with a student count greater than or equal to 5.
# Write your MySQL query statement below
SELECT class
FROM Courses
GROUP BY 1
HAVING COUNT(1) >= 5;Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Derived Table Filter | O(n) | O(k) | When you need to reuse the per-class count in other logic |
| GROUP BY with HAVING | O(n) | O(k) | General case and interviews. Shortest correct query |
Frequently asked questions
Is Classes With at Least 5 Students easy or hard?
This problem is easy. Its acceptance rate is about 65 percent, and the full solution is one GROUP BY query with a HAVING clause. The main pitfall is putting the condition in WHERE instead of HAVING, which causes an error because WHERE runs before aggregation.
Classes With at Least 5 Students Python/Java solution?
This is a SQL problem, so there is no Python or Java solution to write. Submit the MySQL query GROUP BY class HAVING COUNT(DISTINCT student) >= 5. If you use pandas instead, call groupby('class') on the DataFrame, use nunique() on the student column, and filter the result to values of at least 5.
How to solve Classes With at Least 5 Students in O(n)?
Group the rows by class and count the distinct students in each group. Use HAVING COUNT(DISTINCT student) >= 5 to keep only the groups with at least five students. Select the class column from the result. Because grouping uses a hash-based aggregation, one pass over the table is enough.
What is the best approach for Classes With at Least 5 Students?
The best approach is GROUP BY class with HAVING COUNT(DISTINCT student) >= 5. It aggregates each class in a single pass and filters the groups in the same query. It runs in O(n) time and O(k) space, where n is the number of rows and k is the number of distinct classes.
What data structure is used in Classes With at Least 5 Students?
The query relies on the database's internal hash aggregation to group rows by class. It keeps one counter per class in memory, so the working set grows with the number of distinct classes. In SQL you do not write the data structure yourself, but this is what the engine uses under the hood.
What is the time complexity of Classes With at Least 5 Students?
The optimal query runs in O(n) time, where n is the number of rows in the Courses table. The database reads each row once to build the groups. Filtering the k groups adds O(k), which is never larger than n.
Is Classes With at Least 5 Students asked at Google, Amazon, or Meta?
This problem is tagged Database on LeetCode and is a common warm-up for SQL screens, so you may see similar aggregation questions in SQL rounds at many companies. Specific company tags for this problem are not confirmed here. Practice GROUP BY, HAVING, and COUNT(DISTINCT) because they cover most aggregation questions in those interviews.
