概述转载地址为:http://www.cnblogs.com/finejob/articles/974900.html题目1:======为管理岗位业务培训信息,建立3个表: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 SWhere [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 SWhere [S#] NOT IN ( Select [S#] FROM SC Where [C#]='C5')4. 使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位网上流传的错误答案:Select SN,SD FROM SWhere [S#] IN ( Select [S#] FROM SCRIGHT JOIN C ON SC.[C#]=C.[C#] GROUP BY [S#] HAVING COUNT(*)=COUNT([S#]) )经过调试验证的正确答案: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 SC6. 查询选修课程超过5门的学员学号和所属单位Select SN,SD FROM SWhere [S#] IN ( Select [S#] FROM SC GROUP BY [S#] HAVING COUNT( DISTINCT [C#] ) > 5 )题目2:======已知关系模式:S (SNO,SNAME) 学生关系。SNO 为学号,SNAME 为姓名C (CNO,CNAME,CTEACHER) 课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩1. 找出没有选修过“李明”老师讲授课程的所有学生姓名 Select SNAME FROM SWhere NOT EXISTS ( Select * FROM SC,C Where SC.CNO=C.CNO AND CNAME='李明' AND SC.SNO=S.SNO)2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩Select S.SNO,S.SNAME,AVG_SCGRADE=AVG(SC.SCGRADE)FROM S , SC , (Select SNOFROM SC Where SCGRADE<60 GROUP BY SNO HAVING COUNT(DISTINCT CNO)>=2) A Where S.SNO=A.SNO AND SC.SNO=A.SNOGROUP BY S.SNO,S.SNAME3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名Select S.SNO,S.SNAMEFROM S, (Select SC.SNO FROM SC,C Where SC.CNO=C.CNO AND C.CNAME IN('1','2') GROUP BY SNO HAVING COUNT(DISTINCT CNO)=2 )SC Where S.SNO=SC.SNO4. 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号 Select S.SNO,S.SNAMEFROM S, (Select SC1.SNOFROM SC SC1,C C1,SC SC2,C C2 Where SC1.CNO=C1.CNO AND C1.NAME='1' AND SC2.CNO=C2.CNO AND C2.NAME='2' AND SC1.SCGRADE>SC2.SCGRADE ) SC Where S.SNO=SC.SNO5. 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩Select S.SNO,S.SNAME,SC.[1号课成绩],SC.[2号课成绩] FROM S, ( Select SC1.SNO,[1号课成绩]=SC1.SCGRADE,[2号课成绩]=SC2.SCGRADE FROM SC SC1,C C1,SC SC2,C C2 Where SC1.CNO=C1.CNO AND C1.NAME='1' AND SC2.CNO=C2.CNO AND C2.NAME='2' AND SC1.SCGRADE>SC2.SCGRADE) SC Where S.SNO=SC.SNO题目3:======有如下表记录:ID Name EmailAddress LastLogon100 test4 test4@yahoo.cn 2007-11-25 16:31:2613 test1 test1@yahoo.cn 2007-3-22 16:27:0719 test1 test1@yahoo.cn 2007-10-25 14:13:4642 test1 test1@yahoo.cn 2007-11-20 14:20:1045 test2 test2@yahoo.cn 2007-4-25 14:17:3949 test2 test2@yahoo.cn 2007-5-25 14:22:36用一句sql查询出每个用户最近一次登录的记录(每个用户只显示一条最近登录的记录)方法一:SELECT a.* from users a inner join (SELECT [Name], LastLogon=MAX(LastLogon) FROM users GROUP BY [Name]) b on a.[Name]=b.[Name] and a.[LastLogon]=b.[LastLogon]方法二:SELECT a.* from users a inner join (SELECT Name,MAX(LogonID) LogonID FROM users GROUP BY [Name]) b on a.LogonID=b.LogonID --where a.LogonId=b.LogonId
为管理岗位业务培训信息,建立3个表: S (S#,SN,SD,SA) S#,SA 分别代表学号、学员姓名、所属单位、学员年龄 C (C#,CN ) C#,CN 分别代表课程编号、课程名称 SC ( S#,C#,G ) S#,G 分别代表学号、所选修的课程编号、学习成绩
网上流传的错误答案:
已知关系模式: S (SNO,Sname) 学生关系。SNO 为学号,Sname 为姓名 C (CNO,Cname,CTEACHER) 课程关系。CNO 为课程号,Cname 为课程名,CTEACHER 为任课教师 SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩