FROM SC
WHERE C# IN (SELECT C# FROM C
WHERE TEACHER=’LIU’))
或:
SELECT SNAME (存在量词方式) FROM S
WHERE SEX=’F’ AND EXISTS(SELECT* FROM SC
WHERE SC.S#=S.S# AND EXISTS(SELECT * FROM C
WHERE C.C#=SC.C# AND TEACHER=’LIU’))
⑸ SELECT C# FROM C
WHERE NOT EXISTS (SELECT * FROM S,SC
WHERE S.S#=SC.S# AND SC.C#=C.C# AND SNAME=’WANG)); ⑹ SELECT DISTINCT X.S# FROM SC AS X,SC AS Y
WHERE X.S#=Y.S# AND X.C#!=Y.C#; ⑺ SELECT C#.CNAME FROM C
WHERE NOT EXISTS (SELECT * FROM S
WHERE NOT EXISTS (SELECT * FROM SC
WHERE S#=S.S# AND C#=C.C#)); ⑻ SELECT DISTINCT S#
FROM SC AS X
WHERE NOT EXISTIS
(SELECT *
FROM C
WHERE TEACHER=’LIU’ AND NOT EXISTS (SELECT * FROM SC AS Y
WHERE Y.S#=X.S# AND Y.C#=C.C#));
4-3 试用SQL查询语句表达下列对4.2题中教务管理数据库的三个基本表S、SC、C查询: ⑴ 统计有学生选修的课程门数。
⑵ 求选修4号课程的学生的平均年龄。
⑶ 求LIU老师所授课程的每门课程的学生平均成绩。
第 8 页
⑷ 统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。 ⑸ 检索学号比WANG同学大,而年龄比他小的学生姓名。 ⑹ 检索姓名以WANG打头的所有学生的姓名和年龄。 ⑺ 在SC中检索成绩为空值的学生学号和课程号。 ⑻ 求年龄大于女同学平均年龄的男学生姓名和年龄。
⑼ 求年龄大于所有女同学年龄的男学生姓名和年龄。
解:
⑴ SELECT COUNT(DISTINCT C#) FROM SC;
⑵ SELECT AVG(AGE) FROM S,SC
WHERE S.S#=SC.S# AND C#=’4’AND SEX=’F’; ⑶ SELECT C.C#,AVG(GRADE) FROM SC,C
WHERE SC.C#=C.C# AND TEACHER=’LIU’; ⑷ SELECT C#,COUNT(S#) FROM SC
GROUP BY C#
HAVING COUNT(*)>10
ORDER BY 2 DESC,1; ⑸ SELECT SNAME FROM S
WHERE S#>ALL(SELECT S# FROM S
WHERE SNAME=’WANG’
AND AGE FROM S WHERE SNAME=’WANG’); ⑹ SELECT SNAME,AGE FROM S WHERE SNAME LIKE ‘WANG%’ ⑺ SELECT S#,C# FROM SC WHERE GRADE IS NULL; ⑻ SELECT SNAME,AGE FROM S WHERE SEX=’M’AND AGE>(SELECT AVG(AGE) FROM S WHERE SEX=’F’); ⑼ SELECT SNAME,AGE FROM S WHERE SEX=’M’AND AGE>ALL(SELECT AGE FROM S 第 9 页 WHERE SEX=’F’); 4-4 试用SQL更新语句表达对4.2给出的教务管理数据库中三个基本表S、SC、C进行如下更新操作: ⑴ 往基本表S中插入一个学生元组(‘200912143’,‘张晶’,21)。 ⑵ 在基本表S中检索每一门课程成绩都大于等于80分的学生学号、姓名和性别,并把检索到的值送往另一个已存在的基本表STUDENT(SNO,SNAME,SEX)。 ⑶ 在基本表SC中删除尚无成绩的选课元组。 ⑷ 把张成民同学在SC中的选课记录全部删去。 ⑸ 把选修高等数学课程中不及格的成绩全部改为空值。 ⑹ 把低于总平均成绩的女同学成绩提高5%。 ⑺ 在基本表SC中修改4号课程的成绩,若成绩小于等于75分时提高5%,若成绩大于75分时提高4%(用两个UPDATE语句实现)。 解: ⑴INSERT INTO S(S#,SNAME,AGE) VALUES(‘200912143’,’张晶’,21); INSERT INTO STUDENT⑵(SNO,SNAME,SEX) SELECT S#,SNAME,SEX FROM S WHERE S# IN (SELECT S# FROM SC WHERE 80<=ALL(SELECT GRADE FROM SC GROUP BY S#)); DELETE FROM SC⑶ WHERE GRADE IS NULL; DELE⑷TE FROM SC WHERE S# IN(SELECT S# FROM S WHERE SNAME=’张民’) ⑸UPDATE SC SET GRADE=NULL WHERE GRADE<60 AND C# IN(SELECT C# FROM C WHERE CNAME=’高等数学’); ⑹UPDATE SC SET GRADE=GRADE*1.05 WHERE S# IN(SELECT S# FROM S WHERE SEX=’F’) AND GRADE<(SELECT AVG(GRADE) FROM SC); ⑺ 用两个UPDATE语句实现: UPDATE SC