主观题:SQL查询
有如下关系:
学生关系S(sno,sname,ssex,sage,sdept), 例:('2020001','李强',’男',20,'计算机')
课程关系C (cno,cname,ccredit,teacher),例:('1137','数据库原理',3,'王华')
学生选课关系SC(sno,cno,grade)。例:('2020001','1137',83)其中sno是学生号,sname是学生姓名,ssex是学生性别,sage是学生年龄,sdept是学生所在系, cno是课程号,cname是课程名称,ccredit是课程学分 ,teacher是教授课程的教师,grade是成绩。每小题用SQL完成下列问题。共5小题,每小题3分。
(1)查询计算机系学生的详细信息。
(2)查询年龄在18-20岁(包括18岁和20岁)的学生的姓名、系别和出生日期。
(3)统计每门课程的选课人数。
(4)查询王华选修的课程的课程号和成绩,并按成绩降序排序。
(5)查询没有选修数据库原理的学生的学号和姓名。
答案:每小题3分,共15分。
(1)select * from S where sdept='计算机';
(2)select sname,sdept,2022-sage from S where sage between 18 and 20;
(3)select cno,count(* ) from SC group by cno;
(4)select cno,grade from S,SC where S.sno=SC.sno and sname='王华' order by grade;
(5)select sno,sname from S where sno not in (select sno from SC,C where SC.cno=C.cno and cname='数据库原理');
学生关系S(sno,sname,ssex,sage,sdept), 例:('2020001','李强',’男',20,'计算机')
课程关系C (cno,cname,ccredit,teacher),例:('1137','数据库原理',3,'王华')
学生选课关系SC(sno,cno,grade)。例:('2020001','1137',83)其中sno是学生号,sname是学生姓名,ssex是学生性别,sage是学生年龄,sdept是学生所在系, cno是课程号,cname是课程名称,ccredit是课程学分 ,teacher是教授课程的教师,grade是成绩。每小题用SQL完成下列问题。共5小题,每小题3分。
(1)查询计算机系学生的详细信息。
(2)查询年龄在18-20岁(包括18岁和20岁)的学生的姓名、系别和出生日期。
(3)统计每门课程的选课人数。
(4)查询王华选修的课程的课程号和成绩,并按成绩降序排序。
(5)查询没有选修数据库原理的学生的学号和姓名。
答案:每小题3分,共15分。
(1)select * from S where sdept='计算机';
(2)select sname,sdept,2022-sage from S where sage between 18 and 20;
(3)select cno,count(* ) from SC group by cno;
(4)select cno,grade from S,SC where S.sno=SC.sno and sname='王华' order by grade;
(5)select sno,sname from S where sno not in (select sno from SC,C where SC.cno=C.cno and cname='数据库原理');