Classes More Than 5 Students

Easy

Description

Write a SQL query to find classes with at least 5 students. Return class names.

Table:courses
ColumnType
studentTEXT
classTEXT

Examples

Input:CREATE TABLE courses (student TEXT, class TEXT); INSERT INTO courses VALUES ('A', 'Math'), ('B', 'Math'), ('C', 'Math'), ('D', 'Math'), ('E', 'Math'), ('F', 'Science');
Output:Math
Explanation:

Math has 5 students (A, B, C, D, E), meeting the requirement. Science has only 1 student, so it doesn't qualify.

Input:CREATE TABLE courses (student TEXT, class TEXT); INSERT INTO courses VALUES ('Alice', 'Physics'), ('Bob', 'Physics'), ('Carol', 'Physics'), ('David', 'Physics'), ('Eve', 'Physics'), ('Frank', 'Physics'), ('Grace', 'Chemistry'), ('Henry', 'Chemistry'), ('Iris', 'Chemistry'), ('Jack', 'Chemistry'), ('Kate', 'Biology'), ('Liam', 'Biology'), ('Maya', 'Biology');
Output:Physics
Explanation:

Physics has 6 students (≥5), Chemistry has 4 students (<5), and Biology has 3 students (<5). Only Physics meets the criteria.

Input:CREATE TABLE courses (student TEXT, class TEXT); INSERT INTO courses VALUES ('S1', 'Art'), ('S2', 'Art'), ('S3', 'Art'), ('S4', 'Art'), ('S5', 'Art'), ('S6', 'Music'), ('S7', 'Music'), ('S8', 'Music'), ('S9', 'Music'), ('S10', 'Music'), ('S11', 'History'), ('S12', 'History');
Output:Art Music
Explanation:

Both Art and Music have exactly 5 students each (≥5), while History has only 2 students (<5). Both qualifying classes are returned.

Constraints

  • Use GROUP BY with HAVING

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!