LeetCode:. 596 more than five students in class

Topic links: https://leetcode-cn.com/problems/classes-more-than-5-students/

topic

There is a coursestable, 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 havingwords 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.

Guess you like

Origin www.cnblogs.com/hider/p/11746425.html