SQLServer 基础练习题及答案资料

上传人:f****u 文档编号:113651704 上传时间:2019-11-09 格式:PDF 页数:10 大小:496.68KB
返回 下载 相关 举报
SQLServer 基础练习题及答案资料_第1页
第1页 / 共10页
SQLServer 基础练习题及答案资料_第2页
第2页 / 共10页
SQLServer 基础练习题及答案资料_第3页
第3页 / 共10页
SQLServer 基础练习题及答案资料_第4页
第4页 / 共10页
SQLServer 基础练习题及答案资料_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《SQLServer 基础练习题及答案资料》由会员分享,可在线阅读,更多相关《SQLServer 基础练习题及答案资料(10页珍藏版)》请在金锄头文库上搜索。

1、SQL Server 基础练习题基础练习题及答案及答案 【幻天火焰】 目录目录 一、单表查询练习 . 1 二、聚合函数练习 . 3 三、分组查询练习 . 3 四、嵌套查询练习 . 4 五、联接查询练习 . 6 六、外联接查询 . 7 七、补充提高 . 7 一、单表查询练习一、单表查询练习 1、查询,查询学生“张三“的全部基本信息 Select * from A_studentinfo where sname=张三 2、查询,查询学生“张三“和”李四”的基本信息 Select * from A_studentinfo where sname=张三 or sname=李四 3、查询,查询姓“张“学

2、生的基本信息 Select * from A_studentinfo where sname like 张% 4、查询,查询姓名中含有“四“字的学生的基本信息 Select * from A_studentinfo where sname like %四% 5、查询,查询姓名长度为三个字,姓“李” ,且最后一个字是“强”的全部学生信息。 select * from A_studentinfo where sname like 李_强 6、查询,查询姓“张“或者姓”李”的学生的基本信息。 Select * from A_studentinfo where sname like 张% or sna

3、me like 李% 7、查询,查询姓“张“并且“所属省份“是“北京“的学生信息 Select * from A_studentinfo where sname like 张% and province=北京 8、查询,查询“所属省份“是“北京“、”新疆”、”山东”或者“上海“的学生的信息 Select * from A_studentinfo where province in (北京,上海,新疆,山东) 9、查询,查询姓“张“,但是“所属省份“不是“北京“的学生信息 Select * from A_studentinfo where sname like 张% and province !

4、=北京 10、查询,查询全部学生信息,并按照“性别”排序,性别相同的情况下按照“所属省份”排序,所 属省份相同的情况下再按照“班级”排序 select * from A_studentinfo order by sex,province,class 11、查询,查询现有学生都来自于哪些不同的省份 select distinct province as 省份 from A_studentinfo 12、查询,查询没有填写成绩的学生的学号、课程号和成绩 Select * from A_studentcourse where score is null 13、查询,查询全部填写了成绩的学生的选修信息

5、,并按照“成绩”从高到低进行排序 Select * from A_studentcourse where score is not null order by score desc 二、聚合函数练习二、聚合函数练习 1、统计,统计共有多少个学生 Select count (*) as 学生数量 from A_studentinfo 2、统计,统计年龄大于 20 岁的学生有多少个 Select count(*) as 学生数量 from A_studentinfo where (2008-yearofbirth)20 3、统计,统计入学时间在 1980 年至 1982 年的学生人数 select

6、 count(*) as 学生数量 from A_studentinfo where enrollment between 1998-01-01 and 2003-12-30 对比以下查询方式,看看有何不同,为什么? select count(*) as 学生数量 from A_studentinfo where enrollment between 1998 and 2003 4、统计,统计学号为“S001“的学生的平均成绩 Select avg(score) as 平均成绩 from A_studentcourse where sno=S001 5、统计,统计学号为“S001“的学生的总成

7、绩 select sum(score) as 总成绩 from A_studentcourse where sno =S001 6、统计,查询课程号为”C001”的课程的最高成绩 select max(score) as 最高成绩 from A_studentcourse where cno=C001 7、统计,查询所有学生中的最大年龄是多少 select 2009-min(yearofbirth) as 最大年龄 from A_studentinfo 三、三、分组查询分组查询练习练习 1、统计,统计每个课程的选修人数 select cno,count(*) as 学生数量 from A_st

8、udentcourse group by cno 2、统计,统计每个同学的总成绩 select sno,sum(score) as 总成绩 from A_studentcourse group by sno 3、统计,统计每个班级中每种性别的学生人数,并按照班级排序 select class as 班级,sex as 性别, count(*) as 人数 from A_studentinfo group by class,sex order by class 4、统计,统计每门课程的平均成绩,并按照成绩降序排序 Select cno,avg(score) as 平均成绩 from A_stud

9、entcourse group by cno order by avg(score) desc 5、统计,显示有两门以上课程不及格的学生的学号 Select sno as 不及格学生学号 from A_studentcourse where score1 6、统计,统计每个班级中的最大年龄是多少 select class as 班级, 2008-min(yearofbirth) as 最大年龄 from A_studentinfo group by class 四、嵌套查询练习四、嵌套查询练习 1、用子查询实现,查询选修“高等数学”课的全部学生的总成绩 select sum(score) as

10、 高等数学总成绩 from A_studentcourse where cno = ( select cno from A_courseinfo where subject=高等数学 ) 2、用子查询实现,统计,显示学号为“S001“的学生在其各科成绩中,最高分成绩所对应的课程号 和成绩 select score,cno from A_studentcourse where sno=S001 and score = ( select max(score) from A_studentcourse where sno =S001 ) 思考:如果该学号学生有两个课程分数都为最高的 100 分,查询

11、会有什么结果 3、用子查询实现,查询 2 班选修“数据库技术“课的所有学生的成绩之和 select sum(score) as 数据库技术总成绩 from A_studentcourse where cno = ( select cno from A_courseinfo where subject=数据库技术) and sno in ( select sno from A_studentinfo where class=2 ) 4、用子查询实现,查询 3 班“张三“同学的“测试管理“成绩 select score from A_studentcourse where cno= ( selec

12、t cno from A_courseinfo where subject=测试管理 ) and sno in ( select sno from A_studentinfo where class=3 and sname=张三 ) 五、联接查询五、联接查询练习练习 1、查询“张三“的各科考试成绩,要求显示姓名、课程号和成绩 select sname as 姓名,cno as 课程号,score as 成绩 from A_studentinfo,A_studentcourse where A_studentinfo.sno=A_studentcourse.sno and sname=张三 2、

13、查询“张三“的各科考试成绩中,哪科没有记录考试成绩,要求显示姓名、课程号和成绩 select sname as 姓名,cno as 课程号,score as 成绩 from A_studentinfo,A_studentcourse where A_studentinfo.sno=A_studentcourse.sno and sname=张三 and score is null 3、查询“张三“的各门课程成绩,要求显示姓名、课程名称和成绩 select sname as 姓名,subject as 课程名称,score as 成绩 from A_studentinfo,A_courseinf

14、o,A_studentcourse where A_studentcourse.sno=A_studentinfo.sno and A_ o=A_ o and A_studentinfo.sname=张三 4、查询 3 班“张三“的“测试管理“成绩,要求显示姓名、成绩 select sname as 姓名,score as 成绩 from A_studentcourse,A_courseinfo,A_studentinfo where A_ o=A_ o and A_studentcourse.sno=A_studentinfo.sno and subject=测试管理 and class=3

15、 and sname=张三 5、查询所有 2000 年以前入学的,各班男生的各科考试平均成绩 select class as 班级,avg(score) as 男生平均成绩 from A_studentcourse,A_courseinfo,A_studentinfo where A_ o=A_ o and A_studentcourse.sno=A_studentinfo.sno and sex=男 and enrollment20 group by class having sum(2008-yearofbirth)60 order by class 3、从学生表中查询第 2 到 5 条数

16、据(以学号排序) select top 4 * from a_studentinfo where sno not in ( select top 1 sno from a_studentinfo order by sno ) order by sno 4、计算每种产品的剩余库存量 表 1,为产品进货表,产品名称 namechar(10),产品数量 amountint 表 2,为产品出货表,产品名称 namechar(10),产品数量 amountint 业务逻辑:表 1 存储产品的总进货量,表 2 存储每种产品每次的出货量,如产品 A 进货为 100,出货了 3 次, 每次分别为 10、20、30,那么 A 产品的库存就为 40 表 A1 | 表 A2 -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号