sql 复杂查询语句一

在这里插入图片描述
在这里插入图片描述

查询至少选修了两门课程并且平均成绩大于或等于75分的学生的学号、姓名、成绩和所在班级。写出相应的SQL语句

select S.studentNo,studentName,Score,className 
	from Student S join Score on S.studentNo=Score.studentNo 
	join Course on Course.courseNo=Score.courseNo 
	join Class on S.classNo=Class.classNo 
	join (select COUNT(courseName) num,student.studentNo/*连接选棵数*/
		from Student join Score on Student.studentNo=Score.studentNo 
		join Course on Score.courseNo=Course.courseNo group by student.studentNo)
		as U on U.studentNo=S.studentNo 
	join (select AVG(score) gg ,courseNo  /*连接各课程的平均分*/
		from Score join student H on Score.studentNo=H.studentNo 
		group by courseNo) G on G.courseNo=Course.courseNo 
	where num>=2 and gg>=75  /*选择选课数大于等于2,平均分大于75的*/

Ps:COUNT 和 AVG 得放在子查询里才可以

把子查询的结果连接上去之后,再加个where就可以解决复杂查询了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值