复杂数据查询
本题中所用的数据库是第 l 题中所建立的 Study 数据库。
(1)查询所有同学的选课及成绩情况,要求显示学生的学号 s _no、姓名 s_name、课程号 Course_no 和课程的成绩 score。
查询代码:
select Student.s_no,s_name,course_no,score
from Student,Choice
where Student.s_no=Choice.s_no
结果截图:
(2)查询所有同学的选课及成绩情况,要求显示学生的姓名 s _name、课程名称 course_ name、课程的成绩 score,并将查询结果存放到一个新的数据表 new_table 中。
查询代码:
select s_name,course_no,score
into new_table --将查询的结果放在另一个新表中
from Student,Choice
where Student.s_no=Choice.s_no
截图结果:
(3)查询“计算机 99-1”班的同学的选课及成绩情况,要求显示学生的学号 s_ no、姓名 s _name、课程号 course _no、课程名称 course_name、课程的成绩 score。
查询代码:
select Student.s_no,s_name,Course.course_no,score,course_name
from Student,Choice,Course,Class
where Student.s_no=Choice.s_no and Course.course_no=Choice.course_no and class_name='计算机99-2'
截图结果:
(4)查询所有同学的学分情况(假设课程成绩>=60 时可获得该门课程的学分),要求显 示学生的学号 s _no、姓名 s_ name、总学分(将该列定名为:total_score)。
查询代码:
select Student.s_no,s_name,SUM(course_score) as 'total'
from Student,Choice,Course
where Student.s_no=Choice.s_no and Course.course_no=Choice.course_no and score>=60
GROUP BY Student.s_no,s_name
结果截图:
(用 JOIN)
(5)查询所有同学的平均成绩及选课门数,要求显示学生的学号 s_ no、姓名 s_ name、平 均成绩(将该列定名为:average_score)、选课的门数(将该列定名为:choice_num)。
查询代码:
select Student.s_no,s_name,AVG(score) as
'average_score',count(Choice.course_no) as 'choice_num'
from Student,Choice,Course
where Student.s_no=Choice.s_no and Course.course_no=Choice.course_no
GROUP BY Student.s_no,s_name
结果截图:
(6)查询所有选修了课程但未参加考试的所有同学及相应的课程,要求显示学生的学号 S_ no、姓名 s_ name、课程号 course_no、课程名称 course_name。
查询代码:
select Student.s_no,s_name,Course.course_no,course_name
from Student,Choice,Course
where Student.s_no=Choice.s_no and Course.course_no=Choice.course_no and score is null
截图结果:
(7)查询所有选修了课程但考试不及格(假设<60 分为不及格)的所有同学及相应的课 程,要求显示学生的学号 s_no、姓名 s_name、课程号 course_no、课程名称 course _name、课程 成绩 course_score。
查询代码:
select
Student.s_no,s_name,Course.course_no,course_name,course_score,score
from Student,Choice,Course
where Student.s_no=Choice.s_no and Course.course_no=Choice.course_no and score <60
截图结果:
(8)查询选修了课程名为“程序设计语言”的所有同学及成绩情况,要求显示学生的姓名 s_ name、课程的成绩 score。(使用 ANY)
查询代码:
select s_name,score
from Student,Choice,Course
where Student.s_no=Choice.s_no and Course.course_no=Choice.course_no and course_name='程序设计语言'
查询截图:
(9)查询“计算机系”的所有同学及成绩情况,要求显示学生的学号 s_ no、姓名 s _name、 班级名称 class _name、课程号 course _no、课程名称 course_name、课程的成绩 score。
查询代码:
select Student.s_no,s_name,class_name,Course.course_no,course_name,score
from Student,Choice,Course,Class
where Student.s_no=Choice.s_no and Course.course_no=Choice.course_no and Student.class_no=Class.class_no and class_dept='计算机系'
截图结果:
(10)查询所有教师的任课情况,要求显示教师姓名 t _name、担任课程的名称 course _name。
查询代码:
select t_name,course_name
from Teacher,Teaching,Course
where Teacher.t_no=Teaching.t_no and Course.course_no=Teaching.course_no
截图结果:
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- xiaozhentang.com 版权所有 湘ICP备2023022495号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务