sql查询练习参考答案_第1页
sql查询练习参考答案_第2页
sql查询练习参考答案_第3页
sql查询练习参考答案_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

SQL 查询练习参考答案 1.分别查询学生表和学生修课表中的全部数据. select * from student select * from sc 2.查询计算机系的学生的姓名、年龄。 select 姓名,年龄 from student where 所在系=计算机系 3.查询选修了 c01 号课程的学生的学号和成绩。 select 学号,成绩 from sc where 课程号=c01 4.查询成绩在 70 到 80 分之间的学生的学号,课程号和成绩. select 学号,课程号,成绩 from sc where 成绩 between 70 and 80 5.查询计算机系年龄在 18 到 20 之间且性别为“男“ 的学生的姓名和年龄 . select 姓名,年龄 from student where 所在系=计算机系 and 性别=男 and 年龄 between 18 and 20 6.查询 9512101 号学生的修课情况. select * from sc where 学号=9512101 7.查询 c01 号课程成绩最高的分数. select max(成绩) 最高分 from sc where 课程号=c01 select 学号,课程号,成绩 最高分 from sc where 成绩=(select max(成绩) from sc where 课 程号=c01) 8.查询学生都修了哪些课程,要求列出课程号 select distinct 课程号 from sc 9.查询 Northwind 数据库中 orders 表的 OrderID,CustomerID 和 OrderDate,并奖最新的订购 日期(OrderDate)列在前面. use Northwind select OrderDate,OrderID,CustomerID from orders 10.查询 Northwind 数据库中 orders 表的 ShipCountry 列以 B,C,D,F 开始且第三个字符为“a“ 的 OrderID,CustomerID 和 ShipCountry 信息. select OrderID,CustomerID,ShipCountry from orders where ShipCountry like BCDF_a% 11.查询 Northwind 数据库中 orders 表的 ShipCountry 列不以 A,B,C,D,E,F 开始且最后一个 字母是“a“的 OrderID,CustomerID 和 ShipCountry 信息. select OrderID,CustomerID,ShipCountry from orders where ShipCountry like ABCDEF%A 12.查询学生数据库中学生的最大年龄和最小年龄. use sqllx select max(年龄) 最大年龄,min(年龄) 最小年龄 from student 13.查询修了 c02 号课程的所有学生的平均成绩,最高成绩,最低成绩. select avg(成绩) 平均成绩,max(成绩) 最高成绩,min( 成绩) 最低成绩 from sc where 课程号 =c02 14.统计每个系的学生人数. select 所在系, count(*) 人数 from student group by 所在系 15.统计每门课程的修课人数和考试最高分. select 课程号,count(*) 修课人数 ,max(成绩) 最高分 from sc group by 课程号 16.统计每个学生的选课门数,并按选课门数的递增顺序显示结果 select 学号,count(课程号) 选课门数 from sc group by 学号 order by count(课程号) 17.统计各系的修课的学生总数和考试的平均成绩. select 所在系,count(*) 学生总数,avg(成绩) 平均成绩 from student st join sc on st.学号=sc.学 号 group by 所在系 18.查询选课门数超过两门的学生的平均成绩和选课门数. select 学号,count(课程号) 选课门数,avg(成绩) 平均成绩 from sc group by 学号 having count(课程号)2 19.列出总成绩超过 200 分的学生,要求列出学号,总成绩 select 学号,sum(成绩) 总成绩 from sc group by 学号 having sum(成绩)200 20.平均价格超过 12.0 元的书的类型(type),平均价格 ,要求只计算有确定价格的图书的情况。 use pubs select type ,avg(price) 平均价格,max(price) 最高价格 from titles group by type having avg(price)12.0 21.查询 pubs 数据库的 titles 表中版税(royalty)为 10 的每类书的平均价格. select avg(price) 平均价格 from titles where royalty=10 group by type 22.查询 pubs 数据库的 titles 表中每类图书的数目超过 3 本的图书的总价格. select sum(price) 总价格 from titles group by type having count(type)3 23.查询选修了 c02 号课程的学生的姓名和所在系. use sqllx select 姓名,所在系 from student st,sc where st.学号=sc.学号 and sc.课程号=c02 24.查询成绩 80 分以上的学生的姓名,课程号和成绩,并按成绩的降序排列结果. select 姓名,课程号,成绩 from student st,sc where st.学号=sc.学号 and sc.成绩80 order by 成绩 desc -等价于下列命令 select 姓名,课程号,成绩 from student st join sc on st.学号=sc.学号 where 成绩80 order by 成绩 desc 25.查询计算机系男生修了“数据库基础 “的学生的姓名,性别 ,成绩 select 姓名,性别,成绩 from student st , course co,sc where st.学号=sc. 学号 and sc.课程号=co. 课程号 and st.所在系= 计算机系and co.课程名=数据库基础 and st. 性别=男 26.查询学生的选课情况,要求列出每位学生的选课情况(包括未选课的学生) ,并列出学 生的学号,姓名,修课号,修课成绩 select student.学号,姓名,课程号,成绩 from student left join sc on student.学号=sc.学号 27.列出“数据库基础 “课程考试成绩前三名的学号 ,姓名,所在系和考试成绩 . select top 3 st.学号,姓名,所在系 ,成绩 from student st,course co ,sc where st.学号=sc. 学号 and co.课程号=sc.课程号 and 课程名=数据库基础 order by 成绩 desc -或者表示为: select top 3 st.学号,姓名,所在系 ,成绩 from student st join sc on st.学号=sc.学号 join course co on sc.课程号=co. 课程号 where 课程名= 数据库基础 order by 成绩 desc 28 查询哪些学生合选了一门课程,要求列出合选课程的学生的学号和课程号 select distinct s1.学号, s1.课程号 from sc s1 join sc s2 on s1.课程号=s2.课程号 order by s1. 课程号 -29.查询哪些学生的年龄相同,要求列出年龄相同的学生的姓名和年龄 . select distinct s1.姓名,s1. 年龄 from student s1 join student s2 on s1.年龄=s2.年龄 order by s1. 年龄 30.查询哪些课程没有人选,要求列出课程号和课程名. select co.课程号,co.课程名 from course co , sc where co.课程号=sc.课程号 and co.课程号 not in(select 课程号 from sc) 31.查询有考试成绩的所有学生的姓名,修课名称,及考试成绩,要求将查询结果放在一张新的 永久表(假设新表名为 new_sc)中, 新表的列名分别为 student_name,course_name,grade. select 姓名 student_name,课程名 course_name,成绩 grade into new_sc from student st join sc on st.学号=sc.学号 join course co on co.课程号=sc.课程号 where sc.成绩 is not null 32.分别查询信息系和计算机系的学生的姓名,性别,修课名称,修课成绩,并要求将这两个查询 结果合并成一个结果集, 并以系名,姓名,性别,修课名称,修课成绩的顺序显示各列 select st.所在系,st.姓名,st. 性别 ,co.课程名,sc.成绩 from student st,course co,sc where sc. 学号=st. 学号 and sc.课程号=co.课程号 and st.所在系=计算机系 union select st.所在系,st.姓名,st. 性别 ,co.课程名,sc.成绩 from student st,course co,sc where sc. 学号=st. 学号 and sc.课程号=co.课程号 and st.所在系=信息系 33 用子查询实现如下查询: 1)查询选修了 c01 号课程的学生的姓名和所在系 select 姓名,所在系 from student where 学号 in (select 学号 from sc where 课程号=c01) 2)查询数学系成绩分以上的学生的学号,姓名 select 学号 ,姓名 from student where 学号 in (select distinct 学号 from sc where 成绩80) and 所在系=数学系 3)查询计算机系学生所选的课程名. select 课程名 from course where 课程号 in (select 课程号 from sc where 学号 in (select 学号 from student where 所在系=计算机系) 4)查询“VB“课程考试成绩前三名的学生的学号,姓名,所在系. select 学号,姓名,所在系 from student where 学号 in (select top 3 with ties 学号 from sc where 课程号 in(select 课程号 from course where 课程名=VB) order by 成绩 desc) -34. insert into test_t values(null,b1,) insert into test_t values(1,b2,c2) insert into test_t values(2,b3,) insert into test_t values(null,b4,c4) -35 select st.学号,姓名,成绩,所在系 into test_3 from student st,sc where st.学号=sc.学号 and st.所在系=计算机系 and sc.成绩80 -36. delete from sc where 成绩=50 -37 -1) delete from sc where 成绩50 and 学号 in(select 学号 from student where 所在系= 信息系) delete from sc from sc join student on sc.学号=student. 学号 where 所在系= 信息系 and 成绩50 -38 update sc set 成绩=成绩+10 where 课程号=c01 -39 -1) upd

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论