[๋ฆฌํธ์ฝ๋] 596. Classes More Than 5 Students
๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems / https://leetcode.com/problemset/all/ ์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://leetcode.com/problems/classes-more-than-5-students/description/
Classes More Than 5 Students - LeetCode
Can you solve this real interview question? Classes More Than 5 Students - Table: Courses +-------------+---------+ | Column Name | Type | +-------------+---------+ | student | varchar | | class | varchar | +-------------+---------+ (student, class) is the
leetcode.com
Table: Courses
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| student | varchar |
| class | varchar |
+-------------+---------+
(student, class) is the primary key column for this table.
Each row of this table indicates the name of a student and the class in which they are enrolled.
Write an SQL query to report all the classes that have at least five students.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input:
Courses table:
+---------+----------+
| student | class |
+---------+----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+----------+
Output:
+---------+
| class |
+---------+
| Math |
+---------+
Explanation:
- Math has 6 students, so we include it.
- English has 1 student, so we do not include it.
- Biology has 1 student, so we do not include it.
- Computer has 1 student, so we do not include it.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
์ต์ 5๋ช ์ด์์ ํ์์ด ์๋ ํด๋์ค๋ฅผ ๋ชจ๋ ์ถ๋ ฅํด๋ผ
> class ์ปฌ๋ผ ๊ธฐ์ค์ผ๋ก ๊ทธ๋ฃนํ ํ ํ
(์ง๊ณํจ์) COUNT๊ฐ 5 ์ด์์ธ class๋ฅผ ์ถ๋ ฅํด์ผํ๋ฏ๋ก
HAVING์ ์ ์ฌ์ฉํ๋ค.
SELECT class
FROM Courses
GROUP BY 1
HAVING COUNT(*) >= 5;