bocaimao

leetcode:

题目:There is a table courses with columns: student and class

Please list out all classes which have more than or equal to 5 students.

For example, the table:

+---------+------------+
| student | class      |
+---------+------------+
| A       | Math       |
| B       | English    |
| C       | Math       |
| D       | Biology    |
| E       | Math       |
| F       | Computer   |
| G       | Math       |
| H       | Math       |
| I       | Math       |
+---------+------------+

Should output:

+---------+
| class   |
+---------+
| Math    |
+---------+

 Note:

The students should not be counted duplicate in each course.

需要注意的就是表中可能会出现重复数据,此处我们使用distinct去重。

解题思路:

1. 先从表courses中查找出每个课程的学生人数,并使用distinct去除重复的数据。

2.将返回数据,新增列count 存到一个新表中

3.从中筛选出count值大于等于5的class

 

select class from

(select class ,count( distinct student) as count from courses group by class )as c
where count >= 5

分类:

技术点:

相关文章:

  • 2022-02-23
  • 2022-01-14
  • 2021-12-10
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2021-10-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
  • 2021-10-01
  • 2021-08-17
  • 2022-02-25
相关资源
相似解决方案