数据库实验6

实验六:数据表高级查询

实验内容

注:以下所有用表都基于XSKC模式
实验5数据库为基础,请使用T-SQL 语句实现进行以下操作:

  1. 查询名字中第2个字为‘向’的学生姓名和学号及选修的课程号、课程名;
select sname,student.sno,course.cno,cname  
from XSKC.student,XSKC.course,XSKC.sc  
where student.sno = sc.sno AND sc.cno = course.cno AND sname like '_向%'  
  1. 列出选修了‘数学’或者‘大学英语’的学生学号、姓名、所在院系、选修课程号及成绩;
select student.sno,sname,sdept,cno,grade  
from XSKC.student,XSKC.sc  
where student.sno=sc.sno and cno in(  
select cno  
from XSKC.course  
where cname in ('数学','大学英语'))  
  1. 查询与‘张力’(假设姓名唯一)年龄不同的所有学生的信息;
select student.*  
from XSKC.student  
where sage !=(  
select sage  
from XSKC.student  
where sname='张力')  
  1. 按照“学号,姓名,所在院系,已修学分”的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和;
select student.sno 学号,sname 姓名,sdept 所在院系,sum(ccredit) 已修学分  
from XSKC.student,XSKC.course,XSKC.sc  
where student.sno=sc.sno and course.cno=sc.cno and grade>=60  
group by student.sno,sname,sdept  
  1. 查找选修了至少一门和张力选修课程一样的学生的学号、姓名及课程号;
select student.sno,sname,sc.cno  
from XSKC.student,XSKC.sc  
where student.sno = sc.sno and sc.sno in(  
select sc.sno  
from XSKC.student,XSKC.sc  
where cno in(  
select cno  
from XSKC.sc,XSKC.student  
where sc.sno = student.sno and Sname = '张力'  
    )  
)  
  1. 查询只被一名学生选修的课程的课程号、课程名;
select sc.cno,cname  
from XSKC.sc,XSKC.course  
where course.cno=sc.cno and sc.cno in(  
select cno  
from XSKC.sc  
group by cno  
having count(*)=1)  
  1. 使用嵌套查询出选修了“数据结构”课程的学生学号和姓名;
select student.sno,sname  
from XSKC.sc,XSKC.student  
where student.sno=sc.sno and sc.cno in(  
select cno  
from XSKC.course  
where cname='数据结构') 
  1. 使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系;
select sname,sage,sdept  
from XSKC.student  
where sdept!='CS' and sage < any(  
select sage  
from XSKC.student  
where sdept='CS')  
  1. 使用ANY、ALL 查询,列出其他院系中比WM系所有学生年龄小的学生的姓名;
select sname  
from XSKC.student  
where sdept!='WM' and sage < all(  
select sage  
from XSKC.student  
where sdept='WM')  
  1. 分别使用连接查询和嵌套查询,列出与‘张力’在一个院系的学生的信息;
    连接查询:
select b.*  
from XSKC.student a,XSKC.student b  
where a.sname='张力' and a.sdept=b.sdept 

嵌套查询:

select student.*  
from XSKC.student  
where sdept=(  
select sdept  
from XSKC.student  
where sname='张力')  
  1. 使用集合查询列出CS系的学生以及性别为女的学生学号及姓名;
select sno,sname  
from XSKC.student  
where sdept='CS'  
intersect  
select sno,sname  
from XSKC.student  
where ssex='女'  
  1. 使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集;
select * 
from XSKC.student  
where sdept='CS'  
union  
select *  
from XSKC.student  
where sage<=19  
select *  
from XSKC.student  
where sdept='CS'  
except  
select *  
from XSKC.student  
where sage<=19  
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Re:从零开始的代码生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值