SQL查询习题

上传人:ali****an 文档编号:110990756 上传时间:2019-11-01 格式:DOC 页数:8 大小:73.50KB
返回 下载 相关 举报
SQL查询习题_第1页
第1页 / 共8页
SQL查询习题_第2页
第2页 / 共8页
SQL查询习题_第3页
第3页 / 共8页
SQL查询习题_第4页
第4页 / 共8页
SQL查询习题_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《SQL查询习题》由会员分享,可在线阅读,更多相关《SQL查询习题(8页珍藏版)》请在金锄头文库上搜索。

1、一、单表查询练习1、查询,查询学生张三的全部基本信息select *from xsbwhere xm=张三2、查询,查询学生张三和”张四”的基本信息select *from xsbwhere xm=张三or xm=张四3、查询,查询姓张学生的基本信息select *from xsbwhere xm like 张%4、查询,查询姓名中含有四字的学生的基本信息select *from xsbwhere xm like %四%5、查询,查询姓名长度为三个字,姓“李”,且最后一个字是“强”的全部学生信息。select *from xsbwhere xm like 李_强6、查询,查询姓张或者姓”李”的

2、学生的基本信息。select *from xsbwhere xm like 张%or xm=李%7、查询,查询姓张并且所属省份是北京的学生信息select *from xsbwhere xm like 张%and jg=北京8、查询,查询所属省份是北京、”新疆”、”山东”或者上海的学生的信息select *from xsbwhere jg=北京or jg=上海or jg=山东or jg=新疆orselect *from xsb where jg in (北京,上海,山东,新疆)9、查询,查询姓张,但是所属省份不是北京的学生信息select *from xsbwhere xm like 张%an

3、d jg!=北京orselect *from xsbwhere xm like 张%and jg北京orselect *from xsbwhere xm like 张%and jg not like %北京%10、查询,查询全部学生信息,并按照“性别”排序,性别相同的情况下按照“所属省份”排序,所属省份相同的情况下再按照“班级”排序select *from xsborder by xb,jg,bj11、查询,查询现有学生都来自于哪些不同的省份select distinct jgfrom xsb12、查询,查询没有填写成绩的学生的学号、课程号和成绩select xh,kch,cjfrom cjb

4、where cj is null13、查询,查询全部填写了成绩的学生的选修信息,并按照“成绩”从高到低进行排序select *from cjbwhere cj is not null order by cj desc14、找出两个姓张的同学信息,只显示对应学生的姓名,性别和班级Select top 2 xm,xb,bjFrom xsbWhere xm like 张%二、聚合函数练习1、统计,统计共有多少个学生select count (*)from xsb2、统计,统计年龄大于20岁的学生有多少个select count (*)from xsbwhere nl=20orselect count

5、 (*)from xsbwhere year(getdate()-year(csrq)203、统计,统计入学时间在1998年至2000年的学生人数select count (*)from xsbwhere rxrq=1998-1-1 and rxrq=2000-12-31where rxrq between 1998-1-1 and 2000-12-314、统计,统计学号为S001的学生的平均成绩select AVG(cj)from cjbwhere xh=1ORselect CONVERT(DECIMAL(18,2),AVG(cj) |保留小数点两位from cjbwhere xh=15、统

6、计,统计学号为S001的学生的总成绩select sum(cj)from cjbwhere xh=16、统计,查询课程号为”C001”的课程的最高成绩select max(cj)from cjbwhere kch=237、统计,查询所有学生中的最大年龄是多少select max(nl)from xsb三、分组查询练习1、统计,统计每个课程的选修人数select kch,count(*)from cjbgroup by kch2、统计,统计每个同学的总成绩select sum(cj)from cjbgroup by xh3、统计,统计每个班级中每种性别的学生人数,并按照班级排序select bj

7、,xb,count(*)from xsbgroup by bj,xb4、统计,统计每门课程的平均成绩,并按照平均成绩降序排序select kch,AVG(cj)from cjbgroup by kchorder by AVG(cj) desc5、统计,显示有两门以上课程不及格的学生的学号select xhfrom cjbwhere cj=2 分组后的条件判断6、统计,统计每个班级中的最大年龄是多少select bj,SUM(nl)from xsbgroup by bj四、嵌套查询练习1、用子查询实现,查询选修“高等数学”课的全部学生的总成绩 1.课程表查课程号2.查所有高数成绩Select S

8、um( cj)from cjbwhere kch=( Select kchfrom kcbwhere kcm=高等数学)2、用子查询实现,统计,显示学号为S001的学生在其各科成绩中,最高分成绩所对应的课程思考:如果该学号学生有两个课程分数都为最高的100分,查询会有什么结果学号一最高分最高分对应的课程号课程号对应课程名Select max(cj) From cjbWhere xh=1Select kchFrom kcbWhere cj= (select max(cj ) from cjb ) and xh=1Select kcmFrom kcbWhere kch in ( Select kc

9、hFrom kcbWhere cj= (select max(cj ) from cjb ) and xh=1)3、用子查询实现,查询2班选修数据库技术课的所有学生的成绩之和1.班级二班学生Select xhFrom xsbWhere bj=22.课程表里查课程号Select kchFrom kcbWhere kcm=数据库技术Select sum(cj)From cjbWhere xh in( Select xhFrom xsbWhere bj=2) and kch=( Select kchFrom kcbWhere kcm=数据库技术)4、用子查询实现,查询3班张三同学的测试管理成绩1.测

10、试管理的课程号2.张三的学号Select xhFrom xsbWhere xm=张三and bj=3Select kchFrom kcbWhere kcm=测试管理Select cjFrom cjbWhere xh in ( Select xh |in 可能有重名的多个张三From xsbWhere xm=张三) and kcm=( Select kchFrom kcbWhere kcm=测试管理)五、联接查询练习1、查询张三的各科考试成绩,要求显示姓名、课程号和成绩 查张三学号 Select xhFrom xsbWhere xm=张三查学号对应成绩Select cj,kchFrom cjbW

11、here xh in(Select xhFrom xsbWhere xm=张三)Or select xm, kch,cjfrom xsb,cjbwhere xsb.xh=cjb.xhand xm=张三2、查询张三的各科考试成绩中,哪科没有记录考试成绩,要求显示姓名、课程号和成绩查张三学号Select xhFrom xsbWhere xm=张三学号对应的没有成绩的Select cj,kchFrom cjbWhere cj is null Select kchFrom cjbWhere cj=( Select cjFrom cjbWhere cj is null) and xh in (Selec

12、t xhFrom xsbWhere xm=张三)Orselect xm, kch,cjfrom xsb,cjbwhere xsb.xh=cjb.xhand xm=张三and cj is null3、查询张三的各门课程成绩,要求显示姓名、课程名称和成绩查张三学号 学号对应成绩select xm,kcm,cjfrom xsb,cjb,kcbwhere xsb.xh=cjb.xh and cjb.kch=kcb.kchand xm=张三4、查询3班张三的测试管理成绩,要求显示姓名、成绩查3班张三的学号查测试管理的课程号根据学号和课程号查成绩select xm,cjfrom xsb,cjb,kcbwhere xsb.xh=cjb.xh and cjb.kch=kcb.kchand xm=张三and kcm=测试管理 and bj=35、查询所有2000年以前入学的,各班男生的各科考试平均成绩select AVG(cj),bj,kchfrom xsb,cjbwhere xsb.xh=cjb.xhand rxrq2000 and xb=男group by bj,kch六、外联接查询查询”李坚强”所有课程的成绩,并显示学号、姓名、课程号和成绩,没有成绩记录的学号包括:(S009,S010,S011)1、使用左联接2、使用右联接3、对比等值连接 七、补充提高1、查询“张三”比“王

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 其它相关文档

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