sql语句面试题

题目转自:https://zhuanlan.zhihu.com/p/38354000


已知有如下4张表:

学生表:STUDENT(S#,SNAME,SAGE,SSEX)

课程表:COURSE(C#,CNAME,T#)

成绩表:SC(S#,C#,SCORE)

教师表:TEACHER(T#,TNAME)

其中,

1)学生表里的字段含义:

S#代表学号,SNAME代表学生姓名,SAGE代表学生年龄,SSEX代表学生性别

2)课程表里的字段含义:

C#代表课程编号,CNAME代表课程名字,T#代表教师编号,

3)成绩表

S#代表学号,C#代表课程编号,SCORE代表成绩

4)教师表的字段含义:

T#代表教师编号,TNAME代表教师姓名

自己赋值:

student表:


Course表:


Teacher表:

SC表:

由于建表时忘记设置主键,需要增加(sno,cno)一起为主键,方便后期引用,注意,每个字段更新语句后面用逗号分开,否则会报错。

方法一:


  
  
  1. alter table sc
  2. change column sno sno int( 11) not null,
  3. change column cno cno int( 11) not null,
  4. add primary key(sno,cno)

方法二:


  
  
  1. alter table sc
  2. modify sno int( 11) not null,
  3. modify cno int( 11) not null,
  4. add primary key(sno,cno)


1.查询课程编号为“001”的课程比“002”的课程成绩高的所有学生的学号


  
  
  1. select x.sno,x.score,y.score from sc x,sc y
  2. where x.cno= 1001
  3. and y.cno= 1002
  4. and x.sno=y.sno
  5. and x.score > y.score

2.查询平均成绩大于60分的学生的学号和平均成绩


  
  
  1. select sno, avg(score) from sc
  2. group by sno
  3. having avg(score)> 60

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


  
  
  1. select sc.sno,sname, count(cno), sum(score)
  2. from student join sc
  3. on student.sno=sc.sno
  4. group by sc.sno,sname

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


  
  
  1. select count(Tname) from teacher
  2. where Tname like '悟%'

5、查询没学过“悟空”老师课的学生的学号、姓名

(对原始SC表稍作修改,令1,2号学生没有学过悟空的课

delete from sc where sno=1 and cno=1009
  
  
delete from sc where sno=2 and cno=1009
  
  



  
  
  1. select sno,sname from student
  2. where sno not in( select sno from SC where cno in( select cno from course
  3. where tno in( select tno from teacher
  4. where tname= '悟空')))

6、查询学过“悟空”老师所教的所有课的同学的学号、姓名

(对原始表Course,SC稍作修改,让悟空交2门课

insert into course values('1010','Exercise','TS04')
  
  

  
  
  1. insert into sc values
  2. ( '8', '1010', '75'),
  3. ( '9', '1010', '92'),
  4. ( '10', '1010', '80');

)


7、查询学过编号为“1001”的课程并且也学过编号为“1010”的课程的学生的学号、姓名


8、查询课程编号为“1002”的总成绩


  
  
  1. select sum(score) from sc
  2. where cno= '1002'

9、查询所有课程成绩小于60分的学生的学号、姓名


  
  
  1. select sno,sname from student
  2. where sno in( select sno from SC where score < 60)

10、查询没有学全所有课的学生的学号、姓名



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值