Using Common Table Expressions (CTEs) to Aggregate Data
IntuitionThe primary goal is to identify classes that have at least five students. The most efficient way to achieve this is by counting the number of students
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The primary goal is to identify classes that have at least five students. The most efficient way to achieve this is by counting the number of students in each class and filtering those that meet the specified criteria.

# Approach
<!-- Describe your approach to solving the problem. -->
1.Common Table Expression (CTE):

Use a CTE named method to simplify the query and improve readability.

Inside the CTE, count the number of students in each class using the COUNT() function and group the results by class.

2.Main Query:

Select the class from the CTE where the count of students (county) is greater than or equal to 5.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
The time complexity is $$O(n)$$, where $$n$$ is the number of rows in the courses table. This is because we need to scan all the rows to perform the count and grouping operations.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
The space complexity is $$O(n)$$, as we need to store the count of each class in the CTE.


# Code
```mysql []
# Write your MySQL query statement below
with method as(
    select class , count(class) as county
    from courses 
    group by class
)
select class from method where county>=5;
```
with method as(
    select class , count(class) as county
    from courses 
    group by class
)
select class from method where county>=5;

['MySQL']