mysql 创建课程表_MySQL全方位练习(学生表 教师表 课程表 分数表)

这篇博客展示了如何使用MySQL进行多表联查、条件筛选等操作,包括查询特定课程间成绩对比、平均成绩、选课情况、教师教学情况等复杂场景。适合数据库初学者和进阶者学习。
摘要由CSDN通过智能技术生成

48304ba5e6f9fe08f3fa1abda7d326ab.png

1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;

select a.* from

(select * from sc a where a.cno='c001') a,

(select * from sc b where b.cno='c002') b

where a.sno=b.sno and a.score > b.score;

select * from sc a

where a.cno='c001'

and exists(select * from sc b where b.cno='c002' and a.score>b.score and a.sno = b.sno)

******************************************************************

2、查询平均成绩大于60 分的同学的学号和平均成绩;

select sno,avg(score) from sc group by sno having avg(score)>60;

******************************************************************

3、查询所有同学的学号、姓名、选课数、总成绩;

select a.*,s.sname from (select sno,sum(score),count(cno) from sc group by sno) a ,student s where a.sno=s.sno

******************************************************************

4、查询姓“刘”的老师的个数;

select count(*) from teacher where tname like '刘%';

******************************************************************

5、查询没学过“谌燕”老师课的同学的学号、姓名;

select a.sno,a.sname from student a where a.sno not in

(

select distinct s.sno from sc s,

(

select c.* from course c ,

(select tno from teacher t where tname='谌燕') t

where c.tno=t.tno

) b

where s.cno = b.cno

)

select * from student st where st.sno not in

(

select distinct sno from sc s

join course c on s.cno=c.cno

join teacher t on c.tno=t.tno where tname='谌燕'

)

******************************************************************

6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;

select st.* from sc a

join sc b on a.sno=b.sno

join student st on st.sno=a.sno

where a.cno='c001' and b.cno='c002' and st.sno=a.sno;

******************************************************************

7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;

select st.* from student st join sc s on st.sno=s.sno

join course c on s.cno=c.cno

join teacher t on c.tno=t.tno

where t.tname='谌燕'

******************************************************************

8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;

select * from

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值