Skip to content

Latest commit

 

History

History
31 lines (20 loc) · 823 Bytes

Classes More Than 5 Students.md

File metadata and controls

31 lines (20 loc) · 823 Bytes

Hints

Use GROUP BY on class and find the COUNT of students in each class.

Use the HAVING clause since the WHERE clause doesn't support aggregate functions

Notes

If the table was designed properly without duplicate entries, the following code would work:

SELECT class FROM courses
GROUP BY class
HAVING COUNT(class) >= 5

However, the duplicate entries in the table prevent the above code from working properly.

MySQL Solution

We use DISTINCT to remove the duplicate entries.

SELECT class FROM courses
GROUP BY class
HAVING COUNT(DISTINCT student) >= 5

Links