java mysql语句练习_MySQL练习

48304ba5e6f9fe08f3fa1abda7d326ab.png

-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

select st.*,sc.s_score as '语文' ,sc2.s_score '数学'

from student st

left join score sc on sc.s_id=st.s_id and sc.c_id='01'

left join score sc2 on sc2.s_id=st.s_id and sc2.c_id='02'

where sc.s_score>sc2.s_score

-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

select st.*,sc.s_score '语文',sc2.s_score '数学' from student st

left join score sc on sc.s_id=st.s_id and sc.c_id='01'

left join score sc2 on sc2.s_id=st.s_id and sc2.c_id='02'

where sc.s_score

-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

select st.s_id,st.s_name,ROUND(AVG(sc.s_score),2) "平均成绩" from student st

left join score sc on sc.s_id=st.s_id

group by st.s_id having AVG(sc.s_score)>=60

-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

-- (包括有成绩的和无成绩的)

select st.s_id,st.s_name,(case when ROUND(AVG(sc.s_score),2) is null then 0 else ROUND(AVG(sc.s_score),2) end ) "平均成绩" from student st

left join score sc on sc.s_id=st.s_id

group by st.s_id having AVG(sc.s_score)<60 or AVG(sc.s_score) is NULL

-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

select st.s_id,st.s_name,count(sc.c_id) "选课总数",sum(case when sc.s_score is null then 0 else sc.s_score end) "总成绩"

from student st

left join score sc on st.s_id = sc.s_id

group by st.s_id

-- 6、查询"李"姓老师的数量

select t.t_name,count(t.t_id) from teacher t

group by t.t_id having t.t_name like "李%";

-- 7、查询学过"张三"老师授课的同学的信息

select st.* from student st

left join score sc on sc.s_id=st.s_id

left join course c on c.c_id=sc.c_id

left join teacher t on t.t_id=c.t_id

where t.t_name="张三"

-- 8、查询没学过"张三"老师授课的同学的信息

-- 张三老师教的课

select c.* from course c left join teacher t on t.t_id=c.t_id where t.t_name="张三"

-- 有张三老师课成绩的st.s_id

select sc.s_id from score sc where sc.c_id in (select c.c_id from course c left join teacher t on t.t_id=c.t_id where t.t_name="张三")

-- 不在上面查到的st.s_id的学生信息,即没学过张三老师授课的同学信息

select st.* from student st where st.s_id not in(

select sc.s_id from score sc where sc.c_id in (select c.c_id from course c left join teacher t on t.t_id=c.t_id where t.t_name="张三")

)

-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select st.* from student st

inner join score sc on sc.s_id = st.s_id

inner join course c on c.c_id=sc.c_id and c.c_id="01"

where st.s_id in (

select st2.s_id from student st2

inner join score sc2 on sc2.s_id = st2.s_id

inner join course c2 on c2.c_id=sc2.c_id and c2.c_id="02"

)

48304ba5e6f9fe08f3fa1abda7d326ab.png

select a.*

from

student a,

score b,

score c

where

a.s_id = b.s_id

and a.s_id = c.s_id

and b.c_id = '01'

and c.c_id = '02';

48304ba5e6f9fe08f3fa1abda7d326ab.png

-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

select st.* from student st

inner join score sc on sc.s_id = st.s_id

inner join course c on c.c_id=sc.c_id and c.c_id="01"

where st.s_id not in (

select st2.s_id from student st2

inner join score sc2 on sc2.s_id = st2.s_id

inner join course c2 on c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值