您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 数据库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和DML,以便进行数据还原)DDL1.写出上述表的建表语句。2.给出相应的INSERT语句来完成题中给出数据的插入。单表查询3.以class降序输出student的所有记录(student表全部属性)命令:select*fromStudentorderbyclassdesc;4.列出教师所在的单位depart(不重复)。命令:selectdistinctdepartfromTeacher;5.列出student表中所有记录的name、sex和class列命令:selectname,sex,classfromStudent;6.输出student中不姓王的同学的姓名。命令:selectnamefromStudentexceptselectnamefromStudentwherenamelike'王%';或selectnamefromStudentwherenamenotlike'王%';7.输出成绩为85或86或88或在60-80之间的记录(no,cno,degree)命令:selectno,cno,DEGREEfromScorewheredegree=85ordegree=86ordegree=88ordegreebetween60and80;8.输出班级为95001或性别为‘女’的同学(student表全部属性)命令:select*fromStudentwhereclass=95001orsex='女';9.以cno升序、degree降序输出score的所有记录。(score表全部属性)命令:select*fromScoreorderbycnoasc,degreedesc;10.输出男生人数及这些男生分布在多少个班级中命令:selectCOUNT(*),count(distinctclass)fromStudentwheresex='男';11.列出存在有85分以上成绩的课程编号。命令:selectdistinctcnofromScorewheredegree85;12.输出95001班级的学生人数命令:selectCOUNT(*)fromStudentwhereclass=95001;13.输出‘3-105’号课程的平均分命令:selectavg(cast(degreeasfloat))fromScorewherecno='3-105';14.输出student中最大和最小的birthday日期值命令:selectMAX(birthday),MIN(birthday)fromStudent;15.显示95001和95004班全体学生的全部个人信息(不包括选课)。(student表全部属性)命令:select*fromStudentwhereclass=95001orclass=95004;聚合查询16.输出至少有5个同学选修的并以3开头的课程的课程号,课程平均分,课程最高分,课程最低分。命令:selectcno,avg(cast(degreeasfloat)),MAX(degree),MIN(degree)fromScorewherecnolike'3%'groupbycnohavingCOUNT(cno)5;或者:selectcno,AVG(cast(DEGREEasfloat)),MAX(degree),MIN(DEGREE)fromScoregroupbycnohavingCOUNT(cno)=5andcnolike'3%'17.输出所选修课程中最低分大于70分且最高分小于90分的学生学号及学生姓名命令:selectStudent.no,namefromStudentjoinScoreonStudent.no=Score.nogroupbyStudent.no,namehavingMAX(Score.degree)90andMIN(Score.degree)70;18.显示所教课程选修人数多于5人的教师姓名命令:selectnamefromTeacherjoinCourseonTeacher.no=Course.tnowhereCourse.cnoin(selectcnofromScoregroupbycnohavingCOUNT(Score.cno)5);19.输出’95001’班级所选课程的课程号和平均分命令:selectcno,avg(cast(degreeasfloat))fromScorewherenoin(selectnofromStudentwhereclass=95001)groupbycno;或者:selectcno,AVG(cast(degreeasfloat))fromScorejoinStudentonScore.no=Student.nogroupbycno,classhavingclass='95001'20.输出至少有两名男同学的班级编号。命令:selectclassfromStudentwheresex='男'groupbyclasshavingCOUNT(class)=2;或者:selecta.classfrom(select*fromStudentwheresex='男')agroupbya.classhavingCOUNT(a.class)=2多表查询21.列出与108号同学同年出生的所有学生的学号、姓名和生日命令:selectno,name,birthdayfromStudentwhereyear(birthday)=(selectyear(birthday)fromStudentwhereno=108);或者:selectb.no,b.name,b.birthdayfromStudentajoinStudentbondatediff(YEAR,a.birthday,b.birthday)=0anda.no='108'22.列出存在有85分以上成绩的课程名称命令:selectcnamefromCoursewherecnoin(selectdistinctcnofromScorewheredegree85);或selectdistinctcnamefromCoursejoinScoreonCourse.cno=Score.cnowheredegree85;23.列出“计算机系”教师所教课程的成绩表(课程编号,课程名,学生名,成绩)。命令:selectCourse.cno,cname,Student.name,DEGREEfromTeacherjoinCourseonTeacher.no=Course.tnojoinScoreonCourse.cno=Score.cnojoinStudentonScore.no=Student.nowhereTeacher.depart='计算机系';24.列出所有可能的“计算机系”与“电子工程系”不同职称的教师配对信息,要求输出每个老师的姓名(name)和(职称)命令:selecta.name,a.prof,b.name,b.proffrom(selectname,prof,departfromTeacherwheredepart='计算机系'ordepart='电子工程系')ajoin(selectname,prof,departfromTeacherwheredepart='电子工程系'ordepart='计算机系')bonnota.prof=b.profandnota.depart=b.depart;25.列出所有处于不同班级中,但具有相同生日的学生,要求输出每个学生的学号和姓名。(提示:使用datediff函数,具体用法可以参考:)命令:selecta.no,a.name,b.no,b.namefromStudentajoinStudentbonnota.class=b.classanda.birthday=b.birthday;26.显示‘张三’教师任课的学生姓名,课程名,成绩命令:selectStudent.name,cname,DEGREEfromTeacherjoinCourseonTeacher.no=Course.tnojoinScoreonCourse.cno=Score.cnojoinStudentonScore.no=Student.nowhereTeacher.name='张三';27.列出所讲课已被选修的教师的姓名和系别命令:selectdistinctname,departfromTeacherjoinCourseonTeacher.no=Course.tnojoinScoreonCourse.cno=Score.cno;28.输出所有学生的name、no和degree。(degree为空的不输出和为空的输出两种情况)。命令:selectname,Student.no,DEGREEfromStudentleftjoinScoreonStudent.no=Score.no;selectname,Student.no,DEGREEfromStudentjoinScoreonStudent.no=Score.no;29.列出所有任课教师的name和depart。(从课程选修和任课两个角度考虑)命令:selectdistinctname,departfromTeacherjoinCourseonTeacher.no=Course.tno;selectdistinctname,departfromTeacherjoinCourseonTeacher.no=Course.tnojoinScoreonCourse.cno=Score.cno;30.输出男教师所上课程名称。命令:selectcnamefromTeacherjoinCourseonTeacher.no=Course.tnowhereTeacher.sex='男';31.出与“李军”同性别的所有同学的name。命令:selectnamefromStudentwheresex=(selectsexfromStudentwherename='李军');32.输出选修“数据结构”课程的男同学的成绩。命令:selectDEGREEfromScorejoinStudentonScore.no=Student.nojoinCourseonScore.cno=Course.cnowheresex='男'andcname='数据结构';33.列出选修编号为‘3-105’课程并且该门课程成绩比课程‘3-111’的最高分要高的cno,no和degree。命令:selectCourse.cno,Score.no,DEGREEfromCoursejoinScoreonCourse.cno=Score.cnojoinStudentonScore.no=Student.nowhereCourse.cno='3-105'andScore.degree(selectmax(degree)fromScorewhereScore.cno='3-111');子查询34.输出score中成绩最高的学号和课程号命令:selectno,cnofromScorewheredegreein(selectMAX(degree)fromScore);35.输出选修3-105课程,其成绩高于109号同学在此课程所得成绩的所有同学的学号,姓名命令:selectStudent.no,namefro
本文标题:数据库sql查询语句练习2_习题_结果(单世民)
链接地址:https://www.777doc.com/doc-4290656 .html