面试题中的sql 题目 我有几个没做出来 ~!
题目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 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'![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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 [S#] FROM SC RIGHT JOIN
C ON SC.[C#]=C.[C#]
GROUP BY [S#]
HAVING COUNT(*)=COUNT([S#]) )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
经过调试验证的正确答案:
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 )
题目2:
======
已知关系模式:
S (SNO,SNAME) 学生关系。SNO 为学号,SNAME 为姓名
C (CNO,CNAME,CTEACHER) 课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师
SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩
1. 找出没有选修过“李明”老师讲授课程的所有学生姓名
Select SNAME FROM S
Where 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)![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM S , SC ,
(Select SNO FROM SC
Where SCGRADE<60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GROUP BY SNO![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
HAVING COUNT(DISTINCT CNO)>=2
) A
Where S.SNO=A.SNO AND SC.SNO=A.SNO![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GROUP BY S.SNO,S.SNAME
3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
Select S.SNO,S.SNAME
FROM S,
(Select SC.SNO FROM SC,C![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Where SC.CNO=C.CNO![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AND C.CNAME IN('1','2')![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GROUP BY SNO![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
HAVING COUNT(DISTINCT CNO)=2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
)SC
Where S.SNO=SC.SNO
4. 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
Select S.SNO,S.SNAME![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM S,
(Select SC1.SNO
FROM SC SC1,C C1,SC SC2,C C2
Where SC1.CNO=C1.CNO AND C1.NAME='1'![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AND SC2.CNO=C2.CNO AND C2.NAME='2'![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AND SC1.SCGRADE>SC2.SCGRADE ) SC
Where S.SNO=SC.SNO
5. 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
Select S.SNO,S.SNAME,SC.[1号课成绩],SC.[2号课成绩]![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM S,
( Select SC1.SNO,[1号课成绩]=SC1.SCGRADE,[2号课成绩]=SC2.SCGRADE![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM SC SC1,C C1,SC SC2,C C2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Where SC1.CNO=C1.CNO AND C1.NAME='1'![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AND SC2.CNO=C2.CNO AND C2.NAME='2'![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AND SC1.SCGRADE>SC2.SCGRADE
) SC
Where S.SNO=SC.SNO
题目3:
======
有如下表记录:
ID Name EmailAddress LastLogon
100 test4 test4@yahoo.cn 2007-11-25 16:31:26
13 test1 test1@yahoo.cn 2007-3-22 16:27:07
19 test1 test1@yahoo.cn 2007-10-25 14:13:46
42 test1 test1@yahoo.cn 2007-11-20 14:20:10
45 test2 test2@yahoo.cn 2007-4-25 14:17:39
49 test2 test2@yahoo.cn 2007-5-25 14:22:36
用一句sql查询出每个用户最近一次登录的记录(每个用户只显示一条最近登录的记录)
======
为管理岗位业务培训信息,建立3个表:
S (S#,SN,SD,SA) S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄
C (C#,CN ) C#,CN 分别代表课程编号、课程名称
SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩
1. 使用标准SQL嵌套语句查询选修课程名称为 税收基础 的学员学号和姓名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2. 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3. 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4. 使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
)--子查询获得选修全部课程的学生学号
5. 查询选修了课程的学员人数
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6. 查询选修课程超过5门的学员学号和所属单位
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GROUP BY [S#]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
题目2:
======
已知关系模式:
S (SNO,SNAME) 学生关系。SNO 为学号,SNAME 为姓名
C (CNO,CNAME,CTEACHER) 课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师
SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩
1. 找出没有选修过“李明”老师讲授课程的所有学生姓名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(Select SNO FROM SC
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Where S.SNO=A.SNO AND SC.SNO=A.SNO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(Select SC.SNO FROM SC,C
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Where S.SNO=SC.SNO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4. 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(Select SC1.SNO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Where S.SNO=SC.SNO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5. 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM S,
( Select SC1.SNO,[1号课成绩]=SC1.SCGRADE,[2号课成绩]=SC2.SCGRADE
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Where S.SNO=SC.SNO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
题目3:
======
有如下表记录:
ID Name EmailAddress LastLogon
100 test4 test4@yahoo.cn 2007-11-25 16:31:26
13 test1 test1@yahoo.cn 2007-3-22 16:27:07
19 test1 test1@yahoo.cn 2007-10-25 14:13:46
42 test1 test1@yahoo.cn 2007-11-20 14:20:10
45 test2 test2@yahoo.cn 2007-4-25 14:17:39
49 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
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(SELECT Name,MAX(LogonID) LogonID FROM users GROUP BY [Name]) b
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)