Classes More Than 5 Students

easy database sql

Problem

Courses(student, class). Return class names with at least 5 distinct students.

Inputc=[('A','Math'),('B','Math'),('C','Math'),('D','Math'),('E','Math'),('F','Bio')]
Output['Math']
Math has 5 distinct students; Bio has only 1.

SELECT class
FROM Courses
GROUP BY class
HAVING COUNT(DISTINCT student) >= 5;
Time: O(n) Space: O(n)