这是一次面试的时候遇到的题目,平时用起来感觉没什么。但是手写就凉凉了,归根到底还是基础不行呀,所以就写了这篇博客巩固一下。
题目如下:
主要考了一些函数的应用,还有经常遇见的group by,having。
#求出该班级语文的平均分
-- select AVG(score) from score where subject='语文'
#统计该班男生人数
-- select count(sex) from student where sex='男'
#语数英3科平均分在90分以上的同学
-- select name from student where name_id in
-- (select name_id from score GROUP BY name_id HAVING sum(score)>270)
#(group by 使用时如果是有条件的,就得用having, 不得使用where)
这里我用关联查询,查出来的是名字
注:group by 和where的具体使用可以看看我的另外一篇博客
https://blog.csdn.net/Certain_/article/details/89787255
#语数英3科中有2科大于90分的同学
select name_id from score where score>90 group by name_id having count(name_id)>=2
全部代码在这里:
https://github.com/Certain97/Mysql-/tree/master
score表 student表