SQ查询经典40题

上传人:飞*** 文档编号:51024412 上传时间:2018-08-12 格式:PDF 页数:14 大小:15.47KB
返回 下载 相关 举报
SQ查询经典40题_第1页
第1页 / 共14页
SQ查询经典40题_第2页
第2页 / 共14页
SQ查询经典40题_第3页
第3页 / 共14页
SQ查询经典40题_第4页
第4页 / 共14页
SQ查询经典40题_第5页
第5页 / 共14页
点击查看更多>>
资源描述

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

1、SQL SERVER查询经典 40题createdatabase test3 use test3 -1.学生表 createtable student( s# varchar( 10), sname varchar( 10), sage datetime, ssex varchar( 10 ) ); insertinto student values( 01, 赵雷 , 1990-01-01, 男 ) insertinto student values( 02, 钱电 , 1990-12-21, 男 ) insertinto student values( 03, 孙风 , 1990-05-2

2、0, 男 ) insertinto student values( 04, 李云 , 1990-08-06, 女 ) insertinto student values( 05, 周梅 , 1991-12-01, 女 ) insertinto student values( 06, 吴兰 , 1992-03-01, 女 ) insertinto student values( 07, 郑竹 , 1989-07-01, 女 ) insertinto student values( 08, 王菊 , 1990-01-20, 女 ) createtable course( c# varchar( 1

3、0), cname varchar( 10), t# varchar( 10) ); insertinto course values( 01, 语文 , 02) insertinto course values( 02, 数学 , 01) insertinto course values( 03, 英语 , 03) createtable teacher( t# varchar( 10), tname varchar( 10), ); insertinto teacher values( 01, 张兰 ) insertinto teacher values( 02, 李四 ) inserti

4、nto teacher values( 03, 王五 ) createtable SC ( S# varchar( 10), C# varchar( 10), score decimal( 18, 1) ) insertinto sc values( 01,01, 80 ) insertinto sc values( 01,02, 90 ) insertinto sc values( 01,03, 99 ) insertinto sc values( 02,01, 70 ) insertinto sc values( 02,02, 60 ) insertinto sc values( 02,0

5、3, 80 ) insertinto sc values( 03,01, 80 ) insertinto sc values( 03,02, 80 ) insertinto sc values( 03,03, 80 ) insertinto sc values( 04,01, 50 ) insertinto sc values( 04,02, 30 ) insertinto sc values( 04,03, 20 ) insertinto sc values( 05,01, 76 ) insertinto sc values( 05,02, 87 ) insertinto sc values

6、( 06,01, 31 ) insertinto sc values( 06,03, 34 ) insertinto sc values( 07,02, 89 ) insertinto sc values( 07,03, 98 ) go -1 、查询 “01“ 课程比 “02“ 课程成绩高的学生的信息及课程分数-1.1、查询同时存在 “01“ 课程和 “02“ 课程的情况 select st. s#, st . sname , b. score , c. score from student st innerjoin sc b on st. s#=b. s# and b . c# =01 in

7、nerjoin sc c on st. s#=c. s# and c . c# =02 where b . score c. score -1.2、 查询同时存在 “01“ 课程和“02“ 课程的情况和存在 “01“ 课程但 - 可能不存在 “02“ 课程的情况 ( 不存在时显示为 null)(以下存在相同 内容时不再解释 ) select st. s#, st . sname , b. score , c. score from student st leftjoin sc b on st. s#=b. s# and b . c# =01 leftjoin sc c on st. s#=c.

8、 s# and c . c# =02 where b . score isnull( c. score , 0) - ISNULL(check_expression,replacement_value) -2 、查询 “01“ 课程比 “02“ 课程成绩低的学生的信息及课程分数 -2.1、查询同时存在 “01“ 课程和 “02“ 课程的情况 select st. s#, st . sname , b. score , c. score from student st innerjoin sc b on st. s#=b. s# and b . c# =01 innerjoin sc c on

9、st. s#=c. s# and c . c# =02 where b . score 60 orderby 平均成绩 desc -4 、查询平均成绩小于分的同学的学生编号和学生姓名和平均成绩 -4.1、查询在 sc 表存在成绩的学生信息的SQL语句。 -4.2、查询在 sc 表中不存在成绩的学生信息的SQL语句 select st. s#, st . sname , isnull( cast ( avg ( sc . score )as decimal( 18 , 2),0)平均成绩 from student st innerjoin sc on st. s#=sc . s# groupby

10、 st. s# , st . sname havingisnull( cast ( avg ( sc . score )as decimal( 18 , 2),0) 01 -13 、查询和 “01“ 号的同学学习的课程完全相同的其他同学的信息 select st.*from Student st whereS# in( selectdistinctSC. S# fromSC wheresc . S# =90 select c . c# 课程ID , c. cname 课程name, max( sc . score )最高 分, min ( sc . score )最低分 , cast ( av

11、g ( sc . score)asdecimal( 18 , 2)平均分 , cast ( selectcount ( sc . s# ) fromsc wheresc . c# =c. c# and score =60)* 100.0 /( selectcount ( sc . s# )from sc where sc . c# =c. c# )asdecimal( 18 , 2) 及格率(%) , cast ( selectcount ( sc . s# ) fromsc wheresc . c# =c. c# and sc . score =70 and sc . score =80 a

12、nd sc . score =90)* 100.0 /( selectcount ( sc . s# )from sc where sc . c#=c. c# )asdecimal( 18, 2) 优秀率 (%) from course c innerjoin sc on c . c# =sc . c# groupby c . c# , c. cname -24 、查询学生平均成绩及其名次 selectt .*, px = rank ()over ( orderby 平均成绩 desc ) from ( select m . S# 学生编号 ,m. Sname 学生姓名 , isnull( c

13、ast ( avg ( score )asdecimal( 18, 2),0) 平均成绩 from Student m leftjoin SC n on m . S# = n . S# groupby m. S# , m. Sname ) t orderby px select t .*, px = DENSE_RANK ()over ( orderby 平均成 绩 desc )from ( select m . S# 学生编号 ,m. Sname 学生姓名 , isnull( cast ( avg ( score )asdecimal( 18, 2),0) 平均成绩 from Student

14、 m leftjoin SC n on m . S# = n . S# groupby m. S# , m. Sname ) t orderby px -25 、查询各科成绩前三名的记录 -25.1 分数重复时保留名次空缺 select st. s#, st . sname , sc1 . c# , sc1 . score fromstudent st , sc sc1 wherest . s# =sc1 . s# and sc1 . score in ( selecttop 3 score from sc sc2 where sc2 . c# =sc1 . c# orderby sc2 .

15、score desc ) orderby sc1 . c# , sc1 . score desc -25.2 分数重复时不保留名次空缺,合并名次 select*from( select t .*, px = DENSE_RANK ()over ( partitionby c# orderbyscore desc )from sc t) m where px between 1 and 3 orderby m. C# , m. px -26 、查询每门课程被选修的学生数 select c . cname, count ( sc . s# ) from course c innerjoin sc on c . c# =sc . c#

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

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

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