The problem begins by explaining there is a Courses table with two columns: student and class. The class column is qualitative and categorical, used to tag multiple rows with a predefined class type. The composite key for the table is made up of both the student and class columns. The goal is to return a result set of classes that have five or more students, in any order.
This problem is simple and is solved using the GROUP BY clause followed by the HAVING clause. My approach is to use the SELECT clause to choose the column the problem wants, and then use the GROUP BY clause to group the classes so there is one row per class.
Next, I use the HAVING clause with the aggregate function COUNT() to filter the grouped data. The goal is to filter the result set to include only classes where the count of students is greater than or equal to five. The HAVING clause is essential as it allows a programmer to filter aggregated data using aggregate functions.
SELECT class
FROM Courses
GROUP BY class
HAVING COUNT(student) >= 5;
Top comments (0)