In fact, this is a "no primary key" table ,so it is possible to have same line like “A - math” “A - math”;
and in this case, you query should count 1 instead of 2.
here is my query:
group by class
having count(distinct student)>=5;
My solution is as same as yours, but the first time I submitted it returning TLE. After reading this post, I tried again, and got Accepted, miraculous.