日期:2014-05-16  浏览次数:20425 次

数据库常用面试题
题目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'
        
  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#]) )

经过调试验证的正确答案:
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#课程数 即为全部课程
                            &nb