MySQL 多表查询习题

这篇博客提供了三个MySQL表(student, sc, course)的练习题目,包括查询2号课程的先修课、未选课学生、选两门以上课程的学生信息、特定学生特定课程成绩等。此外,还涉及到了视图创建、存储过程设计以及修改数据等操作。" 128077521,15486988,javascript到vue的发展历程与框架对比,"['前端开发', 'javascript', 'Vue', 'MVVM', '框架对比']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三个表的sql文件

链接:https://pan.baidu.com/s/17yCdmnEbRawGyOlaJpLUHw
提取码:wgm4

student表:
在这里插入图片描述
sc表:
在这里插入图片描述
course表:
在这里插入图片描述

习题及对应答案

  1. 查询2号课程的间接先修课

     select cname from course
     where cno=(
     select cpno from course
     where cno=(
     select cpno from course
     where cno='2'))
    
  2. 查找哪些学生没有选课

     select s.id,s.sname from student s left join sc on s.sno=sc.sno
     where grade is null
    
  3. 查找选两门课以上同学的姓名、课程名和成绩

     select sname,cname,grade from student,course,sc
     where student.sno=(select sno from sc
     group by sno
     having count(grade)>2) and student.sno=sc.sno and sc.cno=course.cno
    
  4. 查询王姓同学C++的成绩

     select grade from student s LEFT JOIN sc on s.sno=sc.sno
     where cno=(select cno from course where cname='c++') and sname like '王%'
    
  5. 查询比李大奎c++成绩高的同学(没考c++,当c做了)

     select * from student
     where sno=(
     select sno from sc
     where cno=(select cno from course where cname='c') and grade>
     (
     select grade from sc
     where sno=(select sno from student
     where sname='李大奎') and cno=(select cno from course where cname='c')
     )
     )
    
  6. c++最高分是谁

     select sname from student
     where sno=(
     select sno from sc
     where grade=(
     select max(grade) from sc
     where cno=(select cno from course where cname='c++')))
    
  7. 总分最高是谁

     select sname from student 
     where sno=(
     select sno from sc
     GROUP BY sno
     ORDER BY sum(grade) DESC
     limit 1)
    
  8. 创建一个学生成绩视图

     create view student_gradeA as
     select student.sno,student.sname,course.cname,course.cno,sc.grade FROM
     sc inner join student on sc.sno=student.sno
     inner join course on sc.cno=course.cno
    
  9. 假设开除一名同学的操作是第一步把状态改为0,第二步把该学生所有的成绩设置为零,请设计一个存储过程完成开除李大奎。

     DELIMITER $$
     create  procedure kaichu(in s_name VARCHAR(12))
     COMMENT '开除'
     BEGIN
     	update student set status=0 where sname=s_name;
     	update sc set grade=0 where sno=(select sno from student where sname=s_name);
     END $$
     
     call  kaichu('李大奎');
    
  10. 不用视图完成把李大奎的C语言成绩改成80

    UPDATE sc set grade=80
    where sno=(
    select sno from student
    where sname='李大奎') and cno=(
    select cno from course
    where cname='c')
    
  11. 查询和李大奎选相同课程同学的信息。

    select * from student
    where id=any(
    
    select distinct id from sc
    where cno=any(
    select cno from sc
    where sno=
    (select sno from student where sname='李大奎')))
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值