Classes More Than 5 Students

List out all classes which have more than or equal to 5 students.

Example 1:

Table: Person
+---------+------------+
| student | class      |
+---------+------------+
| A       | Math       |
| B       | English    |
| C       | Math       |
| D       | Biology    |
| E       | Math       |
| F       | Computer   |
| G       | Math       |
| H       | Math       |
| I       | Math       |
| A       | Math       |
+---------+------------+
Result:
+---------+
| class   |
+---------+
| Math    |
+---------+

Approach

Mysql

SELECT class FROM(
SELECT DISTINCT student, class FROM courses) as t 
group by class HAVING count(class)>=5

Oracle

SELECT class FROM(
SELECT DISTINCT student, class FROM courses) 
group by class HAVING count(class)>=5

No comments:

Post a Comment