Search code examples
sqlgroup-bycountsubqueryhaving-clause

How to use distinct keyword in SQL?


I tried the following query :

select class
from courses
WHERE distinct student
having count(class)>=5 ;

Basically I am trying to get all the classes that have count more than 5.On the same time I am trying not to have duplicate students. So it means a student can correspond just to one class. But I am having a syntax error. Any help would be appreciated. Example input: {"headers": {"courses": ["student", "class"]}, "rows": {"courses": [["A", "Math"], ["B", "English"], ["C", "Math"], ["D", "Biology"], ["E", "Math"], ["F", "Math"], ["A", "Math"]]}} Example output: {"headers":["class"],"values":[]}


Solution

  • If you want classes with five or more students, you want aggregation:

    select class
    from courses
    group by class
    having count(*) >= 5