Topic links: https://leetcode-cn.com/problems/classes-more-than-5-students/
topic
There is a courses
table, there are: student (student) and class (course).
Please list all than or equal to 5 students in class.
For example, the table:
+ --------- + ------------ +
| Student | class |
+ --------- + ---------- - +
| A | the Math |
| B | Dictionary Dictionary English |
| C | the Math |
| D | Biology |
| E | the Math |
| F. | Computer |
| G | the Math |
| H | the Math |
| the I | the Math |
+ - -------- + ------------ +
should output:
+---------+
| class |
+---------+
| Math |
+---------+
Note:
Students should not be counted twice in each lesson.
Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/classes-more-than-5-students
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
answer
The Direct Solution
---- oracle ----
/* Write your PL/SQL query statement below */
select b.class
from
(
select class,
count(distinct student) as cnt
from courses
group by class
) b
where cnt >= 5; ---- 469ms
Use having
words simplified.
---- oracle ----
select class
from courses
group by class
having count(distinct student) >= 5; ---- 466ms
Think
This problem a little easier, entry-level, it directly like this.