Interview Questions/SQL/Classes With at Least 5 Students

Classes With at Least 5 Students

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You are given a Courses table that records which students are enrolled in which classes. Write a SQL query to find all classes that have at least 5 students enrolled. Return the result in any order.

Database Schema

Courses

Column NameExample Value
studentA
classMath

Example

Courses

studentclass
AMath
BEnglish
CMath
DBiology
EMath
FComputer
GMath
HMath
IMath

Output

class
Math

Explanation:

Math has 6 students - qualifies. English has 1, Biology has 1, Computer has 1 - none qualify. Only Math is returned.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Courses

studentclass
AMath
BEnglish
CMath
DBiology
EMath
FComputer
GMath
HMath
IMath

Output

class
Math