Classes with more than 5 students is the question asked in many interviews and listed on leetcode at 596.
Let's start with SQL Schema for the table named Courses:
CREATE TABLE Courses (student varchar(255), class varchar(255))
INSERT INTO Courses (student, class) values ('A', 'Math')
INSERT INTO Courses (student, class) values ('B', 'English')
INSERT INTO Courses (student, class) values ('C', 'Math')
INSERT INTO Courses (student, class) values ('D', 'Biology')
INSERT INTO Courses (student, class) values ('E', 'Math')
INSERT INTO Courses (student, class) values ('F', 'Computer')
INSERT INTO Courses (student, class) values ('G', 'Math')
INSERT INTO Courses (student, class) values ('H', 'Math')
INSERT INTO Courses (student, class) values ('I', 'Math')
The table will look like this based on the data of courses:
student | class |
---|---|
A | Math |
B | English |
C | Math |
D | Biology |
E | Math |
F | Computer |
G | Math |
H | Math |
I | Math |
class |
---|
Math |
SELECT class, COUNT(*) FROM courses GROUP BY class;
class | count(*) |
---|---|
Math | 6 |
English | 1 |
Biology | 1 |
Computer | 1 |
SELECT class FROM courses GROUP BY class HAVING COUNT(*)>5;
0 Comments