数据库实验报告2
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验内容与要求
请有选择地实践以下各题。
(1)基于“教学管理”数据库jxgl,使用SQL的查询语句表达下列查询:①检索年龄大于23岁的男学生的学号和姓名;
SELECT Sno,Sname
FROM Student
WHERE Ssex=’男’AND
Sage>23;
②检索至少选修一门课程的女生姓名;
SELECT Sname
FROM Student
WHERE Ssex=’女’AND Sno IN
( SELECT Sno
FROM SC
GROUP BY Sno
HAVING count(*)>=1;
);
③检索王同学不学的课程的课程号;
SELECT Cno;
FROM Course
WHERE Cno NOT IN
( SELECT Cno
FROM Student,SC
WHERE Sname like ’王%’AND Student.Sno=SC.Sno
);
④检索至少选修两门课程的学生学号;
SELECT DISTINCT Sno
FROM SC
GROUP BY Sno
HAVING count(*)>=2;
⑤检索全部学生都选修的课程的课程号与课程名;
SELECT Cno,Cname
FROM Course
WHERE NOT EXISTS
( SELECT *
FROM Student
WHERE NOT EXISTS
( SELECT *
FROM SC
WHERE SC.Sno=Student.Sno AND o=o
)
);
⑥检索选修了所有3学分课程的学生学号;
FROM SC X
WHERE NOT EXISTS
( SELECT *
FROM Course
WHERE Ccredit=3 AND NOT EXISTS
( SELECT *
FROM SC Y
WHERE X.Sno=Y.Sno AND o=o
)
);
(2)基于“教学管理”数据库jxgl,使用SQL的查询语句表达下列查询:
①统计有学生选修的课程门数;
SELECT count(DISTINCT Cno)
FROM SC;
②求选修4号课程的学生的平均年龄;
SELECT AVG(Sage)
FROM Student,SC
WHERE Cno=4 AND Student.Sno=SC.Sno;
③求学分为3的每门课程的学生平均成绩;
SELECT AVG(Grade)
FROM Course,SC
WHERE Ccredit=3 AND o=o
GROUP BY o;
④统计每门课程的学生选修人数,要求超过3人的课程才统计,要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列;
SELECT Cno,count(Sno)
FROM SC
GROUP BY Cno
HAVING count(Sno)>3
ORDER BY count(Sno) DESC,Cno ASC;
⑤检索学号比“王菲”同学大而年龄比他小的学生姓名;
SELECT Sname
FROM Student X
WHERE Sno>
( SELECT Sno
FROM Student Y
WHERE Sname=’王菲’AND Sage>
( SELECT Sage
FROM Student Z
WHERE Sname=’王菲’AND X.Sno=Z.Sno AND Y.Sno=Z.Sno
)
);
⑥检索姓名以“王”打头的所有学生的姓名和年龄;
FROM Student
WHERE Sname LIKE ‘王%’;
⑦在SC中检索成绩为空置的学生学号和课程号;
SELECT Sno,Cno
FROM SC
WHERE Grade is NULL;
⑧求年龄大于女同学平均年龄的男学生姓名和年龄;
SELECT Sname,Sage
FROM Student X
WHERE Ssex=’男’AND Sage>
( SELECT AVG(Sage)
FROM Student
WHERE Ssex=’女’AND X.Sno=Y.Sno
);
⑨求年龄大于所有女同学年龄的男同学姓名和年龄;
SELECT Sname,Sage
FROM Student X
WHERE Ssex=’男’AND Sage>
( SELECT MAX(Sage)
FROM Student Y
WHERE Ssex=’女’AND X.Sno=Y.Sno
);
⑩检索所有比“王华”年龄大的学生姓名,年龄和性别;
SELECT Sname,Sage,Ssex
FROM Student X
WHERE Sage>
( SELECT Sage
FROM Student Y
WHERE Sname=’王华’AND X.Sno=Y.Sno
);
①检索选修“2”课程的学生中成绩最高的学生和学号;
SELECT Sname,SC.Sno
FROM Student,SC
WHERE Cno=2 AND Student.Sno=SC.Sno;
②检索学生姓名和其所选修课程的课程号和成绩;
SELECT Sname,Cno,Grade
FROM Student,SC
WHERE Student.Sno=SC.Sno
GROUP BY Sname;
③检索选修4门以上课程的学生总成绩(不统计不及格的课程),并要求按总成绩的降序排列出来;
SELECT Sno,SUM(Grade)
FROM SC X