5 MySQL Solutions | Finding Classes with 5+ Students
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to find classes with at least 5 students.\n\n# Approach\n Describe your approac
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to find classes with at least 5 students.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Select specific columns from the table.\n\n    > `SELECT` \u2192 This command retrieves data from a database\n\n2. Check if classes have at least 5 students.\n\n    > `HAVING` \u2192 This clause allows filtering of aggregated results produced by `GROUP BY` clause (`WHERE` keyword cannot be used with aggregate functions)\n\n    > `COUNT()` \u2192 This function returns the number of rows\n\n    > `DISTINCT` \u2192 This statement returns only distinct (different) values\n\n3. Group the results by the class to find the number of students in each class.\n\n    > `GROUP BY` \u2192 This command groups rows that have the same values into summary rows, typically to perform aggregate functions on them\n\n# Complexity\n- Time complexity: $O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n`n` is the number of rows in the table. This is because the query processes each row in the table once to count the number of students in each class.\n\n# Code\nIt is possible to create various combinations of solutions by mixing and matching different elements from the provided solutions.\n\n### Solution 1:\n```\nSELECT class\nFROM courses\nGROUP BY class\nHAVING COUNT(DISTINCT student) >= 5;\n```\n\n### Solution 2:\n```\nSELECT class\nFROM courses\nGROUP BY class\nHAVING COUNT(student) >= 5;\n```\n\n### Solution 3:\n```\nSELECT class\nFROM courses\nGROUP BY class\nHAVING COUNT(class) >= 5;\n```\n\n### Solution 4:\n```\nSELECT class\nFROM courses\nGROUP BY class\nHAVING COUNT(*) >= 5;\n```\n\n### Solution 5:\n```\nSELECT class\nFROM courses\nGROUP BY class\nHAVING COUNT(class) > 4;\n```
['MySQL']