S (S#,SN,SD,SA) S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄
C (C#,CN ) C#,CN 分别代表课程编号、课程名称
SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩
1.使用标准SQL嵌套语句查询选修课程名称为“税收基础”的学员学号和姓名。
SELECT SN, SD FROM S WHERE [S#] IN (SELECT [S#] FROM C, SC WHERE C.[C#]=SC.[C#] AND CN=N'税收基础')
2.使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位。
SELECT S.SN, S.SD FROM S, SC WHERE S.[S#]=SC.[S#] AND SC.[C#]='C2'
3.使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位。
SELECT SN, SD FROM S WHERE [S#] NOT IN (SELECT [S#] FROM SC WHERE [C#]='C5')
4.使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位。
SELECT SN, SD FROM S WHERE S# IN ( SELECT SC.S# FROM SC RIGHT JOIN C ON SC.C# = C.C# GROUP BY SC.S# 在结果集中以学生分组,分组后的SC.C#选课数=C.C#课程数,即为全部课程 HAVING COUNT(DISTINCT(SC.C#)) 一个学生同一门课程可能有多条成绩记录,需要DISTINCT= (SELECT COUNT(*)FROM C );HAVING条件不能用COUNT(DISTINCT(SC.C#) ) = COUNT(DISTINCT(C.C#) ) 子查询获得选修全部课程的学生学号
5.查询选修了课程的学员人数。
SELECT 学员人数=COUNT(DISTINCT [S#]) FROM SC
6.查询选修课程超过5门的学员学号和所属单位。
SELECT SN, SD FROM S WHERE [S#] IN ( SELECT [S#] FROM SC GROUP BY [S#] HAVING COUNT(DISTINCT [C#]) > 5 )