mysql sql语句面试经典50题_经典sql面试及答案(50)

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

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

select stu.*, sc1.c, sc1.score, sc2.c, sc2.score

from

(select * from sc where c='01')sc1

,(select * from sc where c='02')sc2

,student stu

where stu.s=sc1.s

and stu.s=sc2.s

and sc1.score>sc2.score

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

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

select stu.s, stu.sname, avg(sc.score) avgscore

from student stu,sc

where stu.s=sc.s

group by stu.S

having avgscore>60

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

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

select stu.s, stu.sname, count(c), sum(score)

from student stu, sc

where stu.s=sc.s

group by sc.S

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

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

select count(*)

from teacher tch

where tch.Tname like '李%'

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

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

select stu.*

from student stu, sc

where stu.s=sc.s and sc.c in

(select co.c

from teacher te,course co

where te.t=co.t and te.Tname='张三')

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

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

select *

from student stu1

where stu1.s not in (

#7、查询学过"张三"老师授课的同学的信息(学号)

select stu.s

from student stu, sc

where stu.s=sc.s and sc.c in

(select co.c

from teacher te,course co

where te.t=co.t and te.Tname='张三')

)

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

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

select stu.*

from student stu,sc

where stu.s=sc.s and sc.c='02' and stu.s in(

#查询学过编号为"01"的同学

select stu.s

from student stu, sc

where stu.s=sc.s and sc.c='01'

)

#自连接方法,sc与自身sc进行连接

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

select stu.*

from student stu, sc sc1, sc sc2

where sc1.c='01' and sc2.c='02' and sc1.s=sc2.s and stu.s=sc1.s

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

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

select stu.*

from student stu,sc

where stu.s=sc.s and sc.c='01' and stu.s not in(

#查询学过编号为"02"的同学

select stu.s

from student stu, sc

where stu.s=sc.s and sc.c='02'

)

11、查询没有学全所有课程的同学的信息

#11、查询没有学全所有课程的同学的信息

select stu.*

from student stu, sc

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值