【刷题】面筋-数据库-查出“张”姓学生中平均成绩大于75分的学生信息
HAVING:
-
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用。
-
HAVING 子句可以让我们筛选分组后的各组数据。
示例
SELECT Websites.name, Websites.url, SUM(access_log.count) AS nums FROM (access_log
INNER JOIN Websites
ON access_log.site_id=Websites.id)
GROUP BY Websites.name
HAVING SUM(access_log.count) > 200;
表信息
- 表名:student_score
- 字段名:name course score
查出“张”姓学生中平均成绩大于75分的学生信息
-
关键点:
- 嵌套select
- “张”姓:%模糊匹配,
name like '张%'
- 平均值使用HAVING,与聚合函数连接:
having avg(score)>75
-
代码:
select * from student where name in (select name from student where name like '张%' group by name having avg(score) > 75)
查询出每门课程的成绩都大于80的学生姓名
SELECT S.name FROM Student S GROUP BY S.name Having MIN(S.score)>=80
其他问题罗列
-
1、请写出SQL,找出所有姓张的学生,并按年龄从小到大排列;
SELECT * FROM TableX WHERE Name LIKE '张%' ORDER BY Age;
-
2、请写出SQL,取出计算机科考成绩不及格的学生;
SELECT * FROM TableX x, TableY y WHERE x.Code = y.Code AND Class = '计算机' AND Score < 60;
-
3、通过等值联接,取出Name、Class、Score,请写出SQL即输出结果
SELECT x.Name, y.Class, y.Score FROM TableX x, TableY y WHERE x.Code = y.Code
-
4、通过外联接,取出每个学生的Name、Class、Score、请写SQL输出结果
Left Out:SELECT x.Name, y.Class, y.Score FROM TableX x, TableY y WHERE x.Code = y.Code(+)
Right Out: SELECT x.Name, y.Class, y.Score FROM TableX x, TableY y WHERE x.Code(+) = y.Code
Full Out:Left join union all right join
-
5、请写SQL,在TableX 表中增加一条学生记录(学号:97005 姓名:赵六 年龄:20);
INSERT INTO TableX(Code, Name, Age) VALUES('97005','赵六',20);
COMMIT;
-
6、李五的年龄记录错了,应该是21,请写SQL,根据主键进行更新;
UPDATE TableX SET Age = 21 WHERE Code in (SELECT Code FROM TableX WHERE Name = '李五')
-
7、请写SQL,删除TableX中没有考试成绩的学生记录,请使用not in条件;
DELETE FROM TableX WHERE Code Not in (SELECT Code FROM TableY WHERE NVL(Score,0) = 0)