Skip to main content

Command Palette

Search for a command to run...

LeetCode 596: Classes with at Least 5 Students

LeetCode 596 Explained: Solutions for Classes with 5 or More Students

Updated
1 min read
LeetCode 596: Classes with at Least 5 Students
V

I'm Varchasv, a Data Engineer working on enterprise data integration.

Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.

What I'm doing:

  • Solving 2 Leetcode problems daily (SQL + DSA).
  • Blogging about each problem.
  • Building in public.

My Goal - Land a better data engineering role by mid-2026.

Follow my journey !!

Date: January 28, 2026
Category: SQL
Time Taken: minute
Difficulty: Easy


Problem Statement

Write a solution to find all the classes that have at least five students.

Link: Classes With at Least 5 Students


My Approach

Since we need to count the students in each class, we will use GROUP BY on the class. To filter out classes that have at least 5 students, we will include that condition in the HAVING clause.


Solution Code

SELECT class
FROM Courses
GROUP BY class
HAVING COUNT(DISTINCT student) >= 5

Key Takeaway: Nothing, very easy problem.

Pattern: Aggregation

Mistakes I Made: Again I forgot that I can use aggregate functions in the HAVING clause and instead I used a Subquery.

Series: 90 Days of Data Engineering Progress: 24/90 problems completed

Tags: #DEQuest #LeetCode #SQL #DataEngineering #BuildInPublic

90 Days of Data Engineering

Part 25 of 48

Solving 90 problems over 18 weeks. Daily posts Mon-Fri. Goal: Switch to development role.

Up next

LeetCode 141: Linked List Cycle

Solving LeetCode 141: Detecting a Cycle in a Linked List