本次实验的目的是对SQL语句中比较基础的部分,SELECT语句中的group by,order by,having,以及集合运算,聚集函数的学习与练习
由于无法上传附件SCHOOL数据库的信息,所以,这里只提交了数据库的表头,以及练习部分:
/*STUDENTS(SID,SNAME,EMAIL,GRADE); TEACHERS(TID,TNAME,EMAIL,SALARY); COURSES(CID,CNAME,HOUR); CHOICES(NO,SID,TID,CID,SCORE);*/ --(1)查询年级为2001的所有学生的名称,按编号升序排列。 SELECT SNAME '学生名称' FROM STUDENTS WHERE GRADE='2001' ORDER BY SID; --(2)查询学生的选课成绩合格的课程成绩,并把成绩换算为积点(60分对应积点为1,每增加1分,积点增加0.1) SELECT SID '学生编号' , SCORE '学生成绩', (SCORE-50)/10 '积点' FROM CHOICES WHERE SCORE>'60'; --(3)查询课时是48或64的课程的名称 SELECT CNAME '课程名称' FROM COURSES WHERE HOUR IN('48','64'); --(4)查询所有课程名称中含有DATA的课程编号 SELECT CID '课程编号' FROM COURSES WHERE CNAME LIKE '%DATA%'; --(5)查询所有选课记录的课程号(不重复显示) SELECT DISTINCT CID '课程号' FROM CHOICES; --(6)统计所有老师的平均工资 SELECT AVG(SALARY) '平均工资' FROM TEACHERS; --*********************************(7)查询所有学生的编号,姓名和平均成绩,按总平均成绩降序排列 SELECT STUDENTS.SID '学生编号' , SNAME '学生姓名' , AVG(SCORE) '平均成绩' FROM CHOICES,STUDENTS WHERE STUDENTS.SID=CHOICES.SID GROUP BY STUDENTS.SID,STUDENTS.SNAME ORDER BY AVG(SCORE)DESC; --(8)统计各个课程的选课人数和平均成绩 SELECT CID '课程编号' , COUNT(NO)'选课人数' , AVG(SCORE)'平均成绩' FROM CHOICES GROUP BY CID; --ORDER BY CID --(9)查询至少选修了三门课程的学生的编号 SELECT SID '编号' FROM CHOICES GROUP BY SID HAVING COUNT(*)>3 --(10)查询编号800009026的学生所选的全部课程的课程名和成绩 SELECT COURSES.CNAME,CHOICES.SCORE FROM CHOICES,COURSES WHERE CHOICES.SID='800009026' AND COURSES.CID=CHOICES.CID; --(11)查询所有选了database的学生的编号 SELECT CHOICES.SID '学生编号' FROM CHOICES,COURSES WHERE COURSES.CNAME='database' AND CHOICES.CID=CHOICES.CID --(12)求出选择了同一个课程的学生对 SELECT X.SID '学生1', Y.SID'学生2' FROM CHOICES X,CHOICES Y WHERE X.CID=Y.CID AND X.NO<Y.NO --(13)求出至少被两名学生选修的课程编号 SELECT CHOICES.CID FROM CHOICES GROUP BY CHOICES.SID,CHOICES.CID HAVING COUNT(SID)>2 --(14)查询选修了编号850955252的学生所选的某个课程的学生编号 SELECT Y.SID FROM CHOICES AS X, CHOICES AS Y WHERE X.CID=Y.CID AND X.SID='850955252' --(15)查询学生的基本信息及选修课程编号和成绩 SELECT STUDENTS.SID,SNAME,STUDENTS.EMAIL,GRADE,CHOICES.CID,SCORE FROM STUDENTS,CHOICES WHERE STUDENTS.SID=CHOICES.SID --(16)查询学号850955252的学生的姓名和选修的课程名称及成绩 SELECT STUDENTS.SNAME,COURSES.CNAME,CHOICES.SCORE FROM STUDENTS,COURSES,CHOICES WHERE STUDENTS.SID=CHOICES.SID AND COURSES.CID=CHOICES.CID AND STUDENTS.SID='850955252' --(17)查询学号850955252的学生同年级的所有学生资料 SELECT * FROM STUDENTS WHERE GRADE IN( SELECT STUDENTS.GRADE FROM STUDENTS WHERE STUDENTS.SID='850955252' ) --(18)查询所有的有选课的学生的详细信息 SELECT * FROM STUDENTS WHERE SID IN( SELECT SID FROM CHOICES ) /* SELECT * FROM STUDENTS WHERE SID IN( SELECT CHOICES.SID FROM CHOICES,STUDENTS WHERE STUDENTS.SID=CHOICES.SID ) */ --(19)查询没有学生选的课程的编号 SELECT CID FROM CHOICES WHERE SID NOT IN( SELECT SID FROM CHOICES ) /* SELECT CID FROM CHOICES WHERE CID NOT IN( SELECT CID FROM CHOICES )这两个一样吗? */ --(20)查询选修了课程名为C++的学生学号和姓名 SELECT STUDENTS.SID,STUDENTS.SNAME,COURSES.CNAME FROM STUDENTS,COURSES,CHOICES WHERE CHOICES.CID=COURSES.CID AND COURSES.CNAME='C++' /* SELECT SID,SNAME FROM STUDENTS WHERE SID IN( SELECT SID FROM CHOICES WHERE CID IN( SELECT CID FROM COURSES WHERE CNAME='C++' ) ) 问题:1.查询结果是否相同? 2.如果结果相同,查询时间为什么不同?*/ --**********************************(21)找出选修课程成绩最差的选课记录 SELECT * FROM CHOICES WHERE CHOICES.SCORE >= ALL( SELECT SCORE FROM CHOICES WHERE SCORE IS NOT NULL ) --(22)找出和课程UML或课程C++的课时一样的课程名称 SELECT CNAME FROM COURSES WHERE HOUR IN( SELECT HOUR FROM COURSES WHERE CNAME='UML' OR CNAME='C++' ) /* SELECT CNAME FROM COURSES WHERE HOUR =SOME( SELECT HOUR FROM COURSES WHERE CNAME='UML' OR CNAME='C++' ) 问题:IN 和 =SOME 的区别在哪里?*/ --(23)查询所有选修编号10001的课程的学生的姓名 SELECT DISTINCT SNAME FROM STUDENTS,CHOICES WHERE STUDENTS.SID=CHOICES.SID AND CID='10001' /* SELECT SNAME FROM STUDENTS WHERE EXISTS( SELECT * FROM CHOICES X WHERE X.CID='10001' AND X.SID=STUDENTS.SID ) 问题:区别在哪里?*/ --**********************************(24)查询选修了所有课程的学生姓名 SELECT SNAME FROM STUDENTS WHERE NOT EXISTS( SELECT * FROM COURSES AS X WHERE NOT EXISTS( SELECT * FROM CHOICES AS Y WHERE Y.SID=STUDENTS.SID AND Y.CID=X.CID ) ) --(25)利用集合运算,查询选修课程C++或课程Java的学生的编号 SELECT CHOICES.SID FROM CHOICES,COURSES WHERE (COURSES.CNAME='C++' OR COURSES.CNAME='JAVA') AND CHOICES.CID=COURSES.CID --(26)实现集合交运算,查询既选修课程C++又选修课程Java的学生的编号 SELECT CHOICES.SID FROM CHOICES,COURSES WHERE (COURSES.CNAME='C++' AND COURSES.CNAME='JAVA') AND CHOICES.CID=COURSES.CID --(27)实现集合减运算,查询选修课程C++而没有选修课程Java的学生的编号 SELECT CHOICES.SID FROM CHOICES,COURSES WHERE (COURSES.CNAME='C++' AND COURSES.CNAME<>'JAVA') AND CHOICES.CID=COURSES.CID