数据库sql查询语句练习2习题结果.docx

上传人:小飞机 文档编号:3559909 上传时间:2023-03-13 格式:DOCX 页数:10 大小:39.14KB
返回 下载 相关 举报
数据库sql查询语句练习2习题结果.docx_第1页
第1页 / 共10页
数据库sql查询语句练习2习题结果.docx_第2页
第2页 / 共10页
数据库sql查询语句练习2习题结果.docx_第3页
第3页 / 共10页
数据库sql查询语句练习2习题结果.docx_第4页
第4页 / 共10页
数据库sql查询语句练习2习题结果.docx_第5页
第5页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《数据库sql查询语句练习2习题结果.docx》由会员分享,可在线阅读,更多相关《数据库sql查询语句练习2习题结果.docx(10页珍藏版)》请在三一办公上搜索。

1、数据库sql查询语句练习2习题结果现在有一教学管理系统,具体的关系模式如下: Student (no, name, sex, birthday, class) Teacher (no, name, sex, birthday, prof, depart) Course (cno, cname, tno) Score (no, cno, degree) 其中表中包含如下数据: Course表: Score表: Student表: Teacher表: 根据上面描述完成下面问题: DDL 1. 写出上述表的建表语句。 2. 给出相应的INSERT语句来完成题中给出数据的插入。 单表查询 3. 以cl

2、ass降序输出student的所有记录 命令:select * from Student order by class desc; 4. 列出教师所在的单位depart。 命令:select distinct depart from Teacher; 5. 列出student表中所有记录的name、sex和class列 命令:select name,sex,class from Student; 6. 输出student中不姓王的同学的姓名。 命令:select name from Student except select name from Student where name like

3、王%;或 select name from Student where name not like 王%; 7. 输出成绩为85或86或88或在60-80之间的记录 命令:select no,cno,DEGREE from Score where degree=85 or degree=86 or degree=88 or degree between 60 and 80; 8. 输出班级为95001或性别为女 的同学 命令:select * from Student where class=95001 or sex=女; 9. 以cno升序、degree降序输出score的所有记录。 命令:

4、select * from Score order by cno asc,degree desc; 10. 输出男生人数及这些男生分布在多少个班级中 命令:select COUNT(*),count(distinct class) from Student where sex=男; 11. 列出存在有85分以上成绩的课程编号。 命令:select distinct cno from Score where degree85; 12. 输出95001班级的学生人数 命令:select COUNT(*) from Student where class=95001; 13. 输出3-105号课程的

5、平均分 命令:select avg(cast(degree as float) from Score where cno=3-105; 14. 输出student中最大和最小的birthday日期值 命令:select MAX(birthday),MIN(birthday) from Student; 15. 显示95001和95004班全体学生的全部个人信息。 命令:select * from Student where class=95001 or class=95004; 聚合查询 16. 输出至少有5个同学选修的并以3开头的课程的课程号,课程平均分,课程最高分,课程最低分。 命令:se

6、lect cno,avg(cast(degree as float),MAX(degree),MIN(degree) from Score where cno like 3% group by cno having COUNT(cno)5; 或者: select cno,AVG(cast(DEGREE as float),MAX(degree),MIN(DEGREE) from Score group by cno having COUNT(cno)=5 and cno like 3% 17. 输出所选修课程中最低分大于70分且最高分小于90分的学生学号及学生姓名 命令:select Stud

7、ent.no,name from Student join Score on Student.no=Score.no group by Student.no,name having MAX(Score.degree)70; 18. 显示所教课程选修人数多于5人的教师姓名 命令:select name from Teacher join Course on Teacher.no=Course.tno where Co in(select cno from Score group by cno having COUNT(So)5); 19. 输出95001班级所选课程的课程号和平均分 命令:sel

8、ect cno,avg(cast(degree as float) from Score where no in(select no from Student where class=95001) group by cno; 或者: select cno,AVG(cast(degree as float) from Score join Student on Score.no=Student.no group by cno,class having class=95001 20. 输出至少有两名男同学的班级编号。 命令:select class from Student where sex=男

9、 group by class having COUNT(class)=2; 或者:select a.class from (select * from Student where sex=男) a group by a.class having COUNT(a.class)=2 多表查询 21. 列出与108号同学同年出生的所有学生的学号、姓名和生日 命令:select no,name,birthday from Student where year(birthday) =(select year(birthday) from Student where no=108); 或者:select

10、 b.no,b.name,b.birthday from Student a join Student b on datediff(YEAR,a.birthday,b.birthday)=0 and a.no=108 22. 列出存在有85分以上成绩的课程名称 命令:select cname from Course where cno in (select distinct cno from Score where degree85);或 select distinct cname from Course join Score on Co=So where degree85; 23. 列出“计

11、算机系”教师所教课程的成绩表。 命令:select Co,cname,Student.name,DEGREE from Teacher join Course on Teacher.no=Course.tno join Score on Co=So join Student on Score.no=Student.no where Teacher.depart=计算机系; 24. 列出所有可能的“计算机系”与“电子工程系”不同职称的教师配对信息,要求输出每个老师的姓名和 命select a.name,a.prof,b.name,b.prof from (select name,prof,dep

12、art from Teacher where depart=计算机系 or depart=电子工程系) a join (select name,prof,depart from Teacher where depart=电子工程系 or depart=计算机系) b on not a.prof=b.prof and not a.depart=b.depart; 令:25. 列出所有处于不同班级中,但具有相同生日的学生,要求输出每个学生的学号和姓名。 命令:select a.no,a.name,b.no,b.name from Student a join Student b on not a.

13、class=b.class and a.birthday=b.birthday; 26. 显示张三教师任课的学生姓名,课程名,成绩 命令:select Student.name,cname,DEGREE from Teacher join Course on Teacher.no=Course.tno join Score on Co=So join Student on Score.no=Student.no where Teacher.name=张三; 27. 列出所讲课已被选修的教师的姓名和系别 命令:select distinct name,depart from Teacher jo

14、in Course on Teacher.no=Course.tno join Score on Co=So; 28. 输出所有学生的name、no和degree。 命令:select name,Student.no,DEGREE from Student left join Score on Student.no=Score.no; select name,Student.no,DEGREE from Student join Score on Student.no=Score.no; 29. 列出所有任课教师的name和depart。 命令:select distinct name,dep

15、art from Teacher join Course on Teacher.no=Course.tno; select distinct name,depart from Teacher join Course on Teacher.no=Course.tno join Score on Co=So; 30. 输出男教师所上课程名称。 命令:select cname from Teacher join Course on Teacher.no=Course.tno where Teacher.sex=男; 31. 出与“李军”同性别的所有同学的name。 命令:select name fr

16、om Student where sex=(select sex from Student where name=李军); 32. 输出选修“数据结构”课程的男同学的成绩。 命令:select DEGREE from Score join Student on Score.no=Student.no join Course on So=Co where sex=男 and cname=数据结构; 33. 列出选修编号为3-105课程并且该门课程成绩比课程 3-111的最高分要高的cno,no和degree。 命令:select Co,Score.no,DEGREE from Course jo

17、in Score on Co=So join Student on Score.no=Student.no where Co=3-105 and Score.degree(select max(degree) from Score where So=3-111); 子查询 34. 输出score中成绩最高的学号和课程号 命令:select no,cno from Score where degree in(select MAX(degree) from Score); 35. 输出选修3-105课程,其成绩高于109号同学在此课程所得成绩的所有同学的学号,姓名 命令:select Studen

18、t.no,name from Student join Score on Student.no =Score.no where So=3-105 and degree(select degree from Score where no=109 and cno=3-105); 36. 列出成绩比该课程平均成绩低的同学的学号,成绩和该门课的平均成绩 命令:select no,DEGREE,a.avg_degree from Score join (select cno,AVG(cast(degree as float) from Score group by cno) a(cno,avg_degr

19、ee) on So=o where Score.degree(select MAX(degree) from Score where cno=4-109); 39. *列出符合下述条件的所有可能的同学配对。其中要求学号为sno1的sname1同学的所学课程的平均分大于学号为sno2的sname2同学的所学课程平均分,两个同学的课程平均分的差值difference为 命令:select a.no,a.name,b.no,b.name,a.avg_degree-b.avg_degree as difference from (select Student.no,name,avg(degree) from Student join Score on Student.no=Score.no group by Student.no,name) a(no,name,avg_degree) join (select Student.no,name,avg(degree) from Student join Score on Student.no=Score.no group by Student.no,name) b(no,name,avg_degree) on a.avg_degreeb.avg_degree;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号