sql语句多表查询(学生表/课程表/教师表/成绩表 )

原创
2013/08/10 22:22
阅读数 5.9K

--问题及描述:
--1.学生表
--Student(S#,Sname,Sage,Ssex) --S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表
--Course(C#,Cname,T#) --C# --课程编号,Cname 课程名称,T# 教师编号
--3.教师表
--Teacher(T#,Tname) --T# 教师编号,Tname 教师姓名
--4.成绩表
--SC(S#,C#,score) --S# 学生编号,C# 课程编号,score 分数

--创建测试数据
CREATE TABLE Student
    (
      S# VARCHAR(10) ,
      Sname NVARCHAR(10) ,
      Sage DATETIME ,
      Ssex NVARCHAR(10)
    )
INSERT  INTO Student
VALUES  ( '01', N'赵雷', '1990-01-01', N'男' )
INSERT  INTO Student
VALUES  ( '02', N'钱电', '1990-12-21', N'男' )
INSERT  INTO Student
VALUES  ( '03', N'孙风', '1990-05-20', N'男' )
INSERT  INTO Student
VALUES  ( '04', N'李云', '1990-08-06', N'男' )
INSERT  INTO Student
VALUES  ( '05', N'周梅', '1991-12-01', N'女' )
INSERT  INTO Student
VALUES  ( '06', N'吴兰', '1992-03-01', N'女' )
INSERT  INTO Student
VALUES  ( '07', N'郑竹', '1989-07-01', N'女' )
INSERT  INTO Student
VALUES  ( '08', N'王菊', '1990-01-20', N'女' )
CREATE TABLE Course
    (
      C# VARCHAR(10) ,
      Cname NVARCHAR(10) ,
      T# VARCHAR(10)
    )
INSERT  INTO Course
VALUES  ( '01', N'语文', '02' )
INSERT  INTO Course
VALUES  ( '02', N'数学', '01' )
INSERT  INTO Course
VALUES  ( '03', N'英语', '03' )
CREATE TABLE Teacher
    (
      T# VARCHAR(10) ,
      Tname NVARCHAR(10)
    )
INSERT  INTO Teacher
VALUES  ( '01', N'张三' )
INSERT  INTO Teacher
VALUES  ( '02', N'李四' )
INSERT  INTO Teacher
VALUES  ( '03', N'王五' )
CREATE TABLE SC
    (
      S# VARCHAR(10) ,
      C# VARCHAR(10) ,
      score DECIMAL(18, 1)
    )
INSERT  INTO SC
VALUES  ( '01', '01', 80 )
INSERT  INTO SC
VALUES  ( '01', '02', 90 )
INSERT  INTO SC
VALUES  ( '01', '03', 99 )
INSERT  INTO SC
VALUES  ( '02', '01', 70 )
INSERT  INTO SC
VALUES  ( '02', '02', 60 )
INSERT  INTO SC
VALUES  ( '02', '03', 80 )
INSERT  INTO SC
VALUES  ( '03', '01', 80 )
INSERT  INTO SC
VALUES  ( '03', '02', 80 )
INSERT  INTO SC
VALUES  ( '03', '03', 80 )
INSERT  INTO SC
VALUES  ( '04', '01', 50 )
INSERT  INTO SC
VALUES  ( '04', '02', 30 )
INSERT  INTO SC
VALUES  ( '04', '03', 20 )
INSERT  INTO SC
VALUES  ( '05', '01', 76 )
INSERT  INTO SC
VALUES  ( '05', '02', 87 )
INSERT  INTO SC
VALUES  ( '06', '01', 31 )
INSERT  INTO SC
VALUES  ( '06', '03', 34 )
INSERT  INTO SC
VALUES  ( '07', '02', 89 )
INSERT  INTO SC
VALUES  ( '07', '03', 98 )
go

--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
--1.1、查询同时存在"01"课程和"02"课程的情况
SELECT  a.* ,
        b.score [课程'01'的分数] ,
        c.score [课程'02'的分数]
FROM    Student a ,
        SC b ,
        SC c
WHERE   a.S# = b.S#
        AND a.S# = c.S#
        AND b.C# = '01'
        AND c.C# = '02'
        AND b.score > c.score
--1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)
SELECT  a.* ,
        b.score [课程"01"的分数] ,
        c.score [课程"02"的分数]
FROM    Student a
        LEFT JOIN SC b ON a.S# = b.S#
                          AND b.C# = '01'
        LEFT JOIN SC c ON a.S# = c.S#
                          AND c.C# = '02'
WHERE   b.score > ISNULL(c.score, 0)

--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
--2.1、查询同时存在"01"课程和"02"课程的情况
SELECT  a.* ,
        b.score [课程'01'的分数] ,
        c.score [课程'02'的分数]
FROM    Student a ,
        SC b ,
        SC c
WHERE   a.S# = b.S#
        AND a.S# = c.S#
        AND b.C# = '01'
        AND c.C# = '02'
        AND b.score < c.score
--2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况
SELECT  a.* ,
        b.score [课程"01"的分数] ,
        c.score [课程"02"的分数]
FROM    Student a
        LEFT JOIN SC b ON a.S# = b.S#
                          AND b.C# = '01'
        LEFT JOIN SC c ON a.S# = c.S#
                          AND c.C# = '02'
WHERE   ISNULL(b.score, 0) < c.score

--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT  a.S# ,
        a.Sname ,
        CAST(AVG(b.score) AS DECIMAL(18, 2)) avg_score
FROM    Student a ,
        sc b
WHERE   a.S# = b.S#
GROUP BY a.S# ,
        a.Sname
HAVING  CAST(AVG(b.score) AS DECIMAL(18, 2)) >= 60
ORDER BY a.S#

--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
--4.1、查询在sc表存在成绩的学生信息的SQL语句。
SELECT  a.S# ,
        a.Sname ,
        CAST(AVG(b.score) AS DECIMAL(18, 2)) avg_score
FROM    Student a ,
        sc b
WHERE   a.S# = b.S#
GROUP BY a.S# ,
        a.Sname
HAVING  CAST(AVG(b.score) AS DECIMAL(18, 2)) < 60
ORDER BY a.S#
--4.2、查询在sc表中不存在成绩的学生信息的SQL语句。
SELECT  a.S# ,
        a.Sname ,
        ISNULL(CAST(AVG(b.score) AS DECIMAL(18, 2)), 0) avg_score
FROM    Student a
        LEFT JOIN sc b ON a.S# = b.S#
GROUP BY a.S# ,
        a.Sname
HAVING  ISNULL(CAST(AVG(b.score) AS DECIMAL(18, 2)), 0) < 60
ORDER BY a.S#

--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
--5.1、查询所有有成绩的SQL。
SELECT  a.S# [学生编号] ,
        a.Sname [学生姓名] ,
        COUNT(b.C#) 选课总数 ,
        SUM(score) [所有课程的总成绩]
FROM    Student a ,
        SC b
WHERE   a.S# = b.S#
GROUP BY a.S# ,
        a.Sname
ORDER BY a.S#
--5.2、查询所有(包括有成绩和无成绩)的SQL。
SELECT  a.S# [学生编号] ,
        a.Sname [学生姓名] ,
        COUNT(b.C#) 选课总数 ,
        SUM(score) [所有课程的总成绩]
FROM    Student a
        LEFT JOIN SC b ON a.S# = b.S#
GROUP BY a.S# ,
        a.Sname
ORDER BY a.S#

--6、查询"李"姓老师的数量
--方法1
SELECT  COUNT(Tname) ["李"姓老师的数量]
FROM    Teacher
WHERE   Tname LIKE N'李%'
--方法2
SELECT  COUNT(Tname) ["李"姓老师的数量]
FROM    Teacher
WHERE   LEFT(Tname, 1) = N'李'


--7、查询学过"张三"老师授课的同学的信息
SELECT DISTINCT
        Student.*
FROM    Student ,
        SC ,
        Course ,
        Teacher
WHERE   Student.S# = SC.S#
        AND SC.C# = Course.C#
        AND Course.T# = Teacher.T#
        AND Teacher.Tname = N'张三'
ORDER BY Student.S#

--8、查询没学过"张三"老师授课的同学的信息
SELECT  m.*
FROM    Student m
WHERE   S# NOT IN ( SELECT DISTINCT
                            SC.S#
                    FROM    SC ,
                            Course ,
                            Teacher
                    WHERE   SC.C# = Course.C#
                            AND Course.T# = Teacher.T#
                            AND Teacher.Tname = N'张三' )
ORDER BY m.S#

--9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
--方法1
SELECT  Student.*
FROM    Student ,
        SC
WHERE   Student.S# = SC.S#
        AND SC.C# = '01'
        AND EXISTS ( SELECT 1
                     FROM   SC SC_2
                     WHERE  SC_2.S# = SC.S#
                            AND SC_2.C# = '02' )
ORDER BY Student.S#
--方法2
SELECT  Student.*
FROM    Student ,
        SC
WHERE   Student.S# = SC.S#
        AND SC.C# = '02'
        AND EXISTS ( SELECT 1
                     FROM   SC SC_2
                     WHERE  SC_2.S# = SC.S#
                            AND SC_2.C# = '01' )
ORDER BY Student.S#
--方法3
SELECT  m.*
FROM    Student m
WHERE   S# IN ( SELECT  S#
                FROM    ( SELECT DISTINCT
                                    S#
                          FROM      SC
                          WHERE     C# = '01'
                          UNION ALL
                          SELECT DISTINCT
                                    S#
                          FROM      SC
                          WHERE     C# = '02'
                        ) t
                GROUP BY S#
                HAVING  COUNT(1) = 2 )
ORDER BY m.S#

--10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
--方法1
SELECT  Student.*
FROM    Student ,
        SC
WHERE   Student.S# = SC.S#
        AND SC.C# = '01'
        AND NOT EXISTS ( SELECT 1
                         FROM   SC SC_2
                         WHERE  SC_2.S# = SC.S#
                                AND SC_2.C# = '02' )
ORDER BY Student.S#
--方法2
SELECT  Student.*
FROM    Student ,
        SC
WHERE   Student.S# = SC.S#
        AND SC.C# = '01'
        AND Student.S# NOT IN ( SELECT  SC_2.S#
                                FROM    SC SC_2
                                WHERE   SC_2.S# = SC.S#
                                        AND SC_2.C# = '02' )
ORDER BY Student.S#

--11、查询没有学全所有课程的同学的信息
--11.1、
SELECT  Student.*
FROM    Student ,
        SC
WHERE   Student.S# = SC.S#
GROUP BY Student.S# ,
        Student.Sname ,
        Student.Sage ,
        Student.Ssex
HAVING  COUNT(C#) < ( SELECT    COUNT(C#)
                      FROM      Course
                    )
--11.2
SELECT  Student.*
FROM    Student
        LEFT JOIN SC ON Student.S# = SC.S#
GROUP BY Student.S# ,
        Student.Sname ,
        Student.Sage ,
        Student.Ssex
HAVING  COUNT(C#) < ( SELECT    COUNT(C#)
                      FROM      Course
                    )

--12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT DISTINCT
        Student.*
FROM    Student ,
        SC
WHERE   Student.S# = SC.S#
        AND SC.C# IN ( SELECT   C#
                       FROM     SC
                       WHERE    S# = '01' )
        AND Student.S# <> '01'

--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT  Student.*
FROM    Student
WHERE   S# IN ( SELECT DISTINCT
                        SC.S#
                FROM    SC
                WHERE   S# <> '01'
                        AND SC.C# IN ( SELECT DISTINCT
                                                C#
                                       FROM     SC
                                       WHERE    S# = '01' )
                GROUP BY SC.S#
                HAVING  COUNT(1) = ( SELECT COUNT(1)
                                     FROM   SC
                                     WHERE  S# = '01'
                                   ) )

--14、查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT  student.*
FROM    student
WHERE   student.S# NOT IN ( SELECT DISTINCT
                                    sc.S#
                            FROM    sc ,
                                    course ,
                                    teacher
                            WHERE   sc.C# = course.C#
                                    AND course.T# = teacher.T#
                                    AND teacher.tname = N'张三' )
ORDER BY student.S#

--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT  student.S# ,
        student.sname ,
        CAST(AVG(score) AS DECIMAL(18, 2)) avg_score
FROM    student ,
        sc
WHERE   student.S# = SC.S#
        AND student.S# IN ( SELECT  S#
                            FROM    SC
                            WHERE   score < 60
                            GROUP BY S#
                            HAVING  COUNT(1) >= 2 )
GROUP BY student.S# ,
        student.sname

--16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT  student.* ,
        sc.C# ,
        sc.score
FROM    student ,
        sc
WHERE   student.S# = SC.S#
        AND sc.score < 60
        AND sc.C# = '01'
ORDER BY sc.score DESC

--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
--17.1 SQL 2000 静态
SELECT  a.S# 学生编号 ,
        a.Sname 学生姓名 ,
        MAX(CASE c.Cname
              WHEN N'语文' THEN b.score
              ELSE NULL
            END) [语文] ,
        MAX(CASE c.Cname
              WHEN N'数学' THEN b.score
              ELSE NULL
            END) [数学] ,
        MAX(CASE c.Cname
              WHEN N'英语' THEN b.score
              ELSE NULL
            END) [英语] ,
        CAST(AVG(b.score) AS DECIMAL(18, 2)) 平均分
FROM    Student a
        LEFT JOIN SC b ON a.S# = b.S#
        LEFT JOIN Course c ON b.C# = c.C#
GROUP BY a.S# ,
        a.Sname
ORDER BY 平均分 DESC
--17.2 SQL 2000 动态
DECLARE @sql NVARCHAR(4000)
SET @sql = 'select a.S# ' + N'学生编号' + ' , a.Sname ' + N'学生姓名'
SELECT  @sql = @sql + ',max(case c.Cname when N''' + Cname
        + ''' then b.score else null end) [' + Cname + ']'
FROM    ( SELECT DISTINCT
                    Cname
          FROM      Course
        ) AS t
SET @sql = @sql + ' , cast(avg(b.score) as decimal(18,2)) ' + N'平均分'
    + ' from Student a left join SC b on a.S# = b.S# left join Course c on b.C# = c.C#
group by a.S# , a.Sname order by ' + N'平均分' + ' desc'
EXEC(@sql)


--24、查询学生平均成绩及其名次
--24.1 查询学生的平均成绩并进行排名,sql 2000用子查询完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
SELECT  t1.* ,
        px = ( SELECT   COUNT(1)
               FROM     ( SELECT    m.S# [学生编号] ,
                                    m.Sname [学生姓名] ,
                                    ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)),
                                           0) [平均成绩]
                          FROM      Student m
                                    LEFT JOIN SC n ON m.S# = n.S#
                          GROUP BY  m.S# ,
                                    m.Sname
                        ) t2
               WHERE    平均成绩 > t1.平均成绩
             ) + 1
FROM    ( SELECT    m.S# [学生编号] ,
                    m.Sname [学生姓名] ,
                    ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)), 0) [平均成绩]
          FROM      Student m
                    LEFT JOIN SC n ON m.S# = n.S#
          GROUP BY  m.S# ,
                    m.Sname
        ) t1
ORDER BY px

SELECT  t1.* ,
        px = ( SELECT   COUNT(DISTINCT 平均成绩)
               FROM     ( SELECT    m.S# [学生编号] ,
                                    m.Sname [学生姓名] ,
                                    ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)),
                                           0) [平均成绩]
                          FROM      Student m
                                    LEFT JOIN SC n ON m.S# = n.S#
                          GROUP BY  m.S# ,
                                    m.Sname
                        ) t2
               WHERE    平均成绩 >= t1.平均成绩
             )
FROM    ( SELECT    m.S# [学生编号] ,
                    m.Sname [学生姓名] ,
                    ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)), 0) [平均成绩]
          FROM      Student m
                    LEFT JOIN SC n ON m.S# = n.S#
          GROUP BY  m.S# ,
                    m.Sname
        ) t1
ORDER BY px
--24.2 查询学生的平均成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
SELECT  t.* ,
        px = RANK() OVER ( ORDER BY [平均成绩] DESC )
FROM    ( SELECT    m.S# [学生编号] ,
                    m.Sname [学生姓名] ,
                    ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)), 0) [平均成绩]
          FROM      Student m
                    LEFT JOIN SC n ON m.S# = n.S#
          GROUP BY  m.S# ,
                    m.Sname
        ) t
ORDER BY px

SELECT  t.* ,
        px = DENSE_RANK() OVER ( ORDER BY [平均成绩] DESC )
FROM    ( SELECT    m.S# [学生编号] ,
                    m.Sname [学生姓名] ,
                    ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)), 0) [平均成绩]
          FROM      Student m
                    LEFT JOIN SC n ON m.S# = n.S#
          GROUP BY  m.S# ,
                    m.Sname
        ) t
ORDER BY px
 
--25、查询各科成绩前三名的记录
--25.1 分数重复时保留名次空缺
SELECT  m.* ,
        n.C# ,
        n.score
FROM    Student m ,
        SC n
WHERE   m.S# = n.S#
        AND n.score IN ( SELECT TOP 3
                                score
                         FROM   sc
                         WHERE  C# = n.C#
                         ORDER BY score DESC )
ORDER BY n.C# ,
        n.score DESC
--25.2 分数重复时不保留名次空缺,合并名次
--sql 2000用子查询实现
SELECT  *
FROM    ( SELECT    t.* ,
                    px = ( SELECT   COUNT(DISTINCT score)
                           FROM     SC
                           WHERE    C# = t.C#
                                    AND score >= t.score
                         )
          FROM      sc t
        ) m
WHERE   px BETWEEN 1 AND 3
ORDER BY m.c# ,
        m.px
--sql 2005用DENSE_RANK实现
SELECT  *
FROM    ( SELECT    t.* ,
                    px = DENSE_RANK() OVER ( PARTITION BY c# ORDER BY score DESC )
          FROM      sc t
        ) m
WHERE   px BETWEEN 1 AND 3
ORDER BY m.C# ,
        m.px

--26、查询每门课程被选修的学生数
SELECT  c# ,
        COUNT(S#) [学生数]
FROM    sc
GROUP BY C#

--27、查询出只有两门课程的全部学生的学号和姓名
SELECT  Student.S# ,
        Student.Sname
FROM    Student ,
        SC
WHERE   Student.S# = SC.S#
GROUP BY Student.S# ,
        Student.Sname
HAVING  COUNT(SC.C#) = 2
ORDER BY Student.S#

--28、查询男生、女生人数
SELECT  COUNT(Ssex) AS 男生人数
FROM    Student
WHERE   Ssex = N'男'
SELECT  COUNT(Ssex) AS 女生人数
FROM    Student
WHERE   Ssex = N'女'
SELECT  SUM(CASE WHEN Ssex = N'男' THEN 1
                 ELSE 0
            END) [男生人数] ,
        SUM(CASE WHEN Ssex = N'女' THEN 1
                 ELSE 0
            END) [女生人数]
FROM    student
SELECT  CASE WHEN Ssex = N'男' THEN N'男生人数'
             ELSE N'女生人数'
        END [男女情况] ,
        COUNT(1) [人数]
FROM    student
GROUP BY CASE WHEN Ssex = N'男' THEN N'男生人数'
              ELSE N'女生人数'
         END

--29、查询名字中含有"风"字的学生信息
SELECT  *
FROM    student
WHERE   sname LIKE N'%风%'
SELECT  *
FROM    student
WHERE   CHARINDEX(N'风', sname) > 0

--30、查询同名同性学生名单,并统计同名人数
SELECT  Sname [学生姓名] ,
        COUNT(*) [人数]
FROM    Student
GROUP BY Sname
HAVING  COUNT(*) > 1

--31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
SELECT  *
FROM    Student
WHERE   YEAR(sage) = 1990
SELECT  *
FROM    Student
WHERE   DATEDIFF(yy, sage, '1990-01-01') = 0
SELECT  *
FROM    Student
WHERE   DATEPART(yy, sage) = 1990
SELECT  *
FROM    Student
WHERE   CONVERT(VARCHAR(4), sage, 120) = '1990'

--32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT  m.C# ,
        m.Cname ,
        CAST(AVG(n.score) AS DECIMAL(18, 2)) avg_score
FROM    Course m ,
        SC n
WHERE   m.C# = n.C#
GROUP BY m.C# ,
        m.Cname
ORDER BY avg_score DESC ,
        m.C# ASC

--33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT  a.S# ,
        a.Sname ,
        CAST(AVG(b.score) AS DECIMAL(18, 2)) avg_score
FROM    Student a ,
        sc b
WHERE   a.S# = b.S#
GROUP BY a.S# ,
        a.Sname
HAVING  CAST(AVG(b.score) AS DECIMAL(18, 2)) >= 85
ORDER BY a.S#

--34、查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT  sname ,
        score
FROM    Student ,
        SC ,
        Course
WHERE   SC.S# = Student.S#
        AND SC.C# = Course.C#
        AND Course.Cname = N'数学'
        AND score < 60

--35、查询所有学生的课程及分数情况;
SELECT  Student.* ,
        Course.Cname ,
        SC.C# ,
        SC.score
FROM    Student ,
        SC ,
        Course
WHERE   Student.S# = SC.S#
        AND SC.C# = Course.C#
ORDER BY Student.S# ,
        SC.C#

--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
SELECT  Student.* ,
        Course.Cname ,
        SC.C# ,
        SC.score
FROM    Student ,
        SC ,
        Course
WHERE   Student.S# = SC.S#
        AND SC.C# = Course.C#
        AND SC.score >= 70
ORDER BY Student.S# ,
        SC.C#

--37、查询不及格的课程
SELECT  Student.* ,
        Course.Cname ,
        SC.C# ,
        SC.score
FROM    Student ,
        SC ,
        Course
WHERE   Student.S# = SC.S#
        AND SC.C# = Course.C#
        AND SC.score < 60
ORDER BY Student.S# ,
        SC.C#

--38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
SELECT  Student.* ,
        Course.Cname ,
        SC.C# ,
        SC.score
FROM    Student ,
        SC ,
        Course
WHERE   Student.S# = SC.S#
        AND SC.C# = Course.C#
        AND SC.C# = '01'
        AND SC.score >= 80
ORDER BY Student.S# ,
        SC.C#

--39、求每门课程的学生人数
SELECT  Course.C# ,
        Course.Cname ,
        COUNT(*) [学生人数]
FROM    Course ,
        SC
WHERE   Course.C# = SC.C#
GROUP BY Course.C# ,
        Course.Cname
ORDER BY Course.C# ,
        Course.Cname

--40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
--40.1 当最高分只有一个时
SELECT TOP 1
        Student.* ,
        Course.Cname ,
        SC.C# ,
        SC.score
FROM    Student ,
        SC ,
        Course ,
        Teacher
WHERE   Student.S# = SC.S#
        AND SC.C# = Course.C#
        AND Course.T# = Teacher.T#
        AND Teacher.Tname = N'张三'
ORDER BY SC.score DESC
--40.2 当最高分出现多个时
SELECT  Student.* ,
        Course.Cname ,
        SC.C# ,
        SC.score
FROM    Student ,
        SC ,
        Course ,
        Teacher
WHERE   Student.S# = SC.S#
        AND SC.C# = Course.C#
        AND Course.T# = Teacher.T#
        AND Teacher.Tname = N'张三'
        AND SC.score = ( SELECT MAX(SC.score)
                         FROM   SC ,
                                Course ,
                                Teacher
                         WHERE  SC.C# = Course.C#
                                AND Course.T# = Teacher.T#
                                AND Teacher.Tname = N'张三'
                       )

--41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
--方法1
SELECT  m.*
FROM    SC m ,
        ( SELECT    C# ,
                    score
          FROM      SC
          GROUP BY  C# ,
                    score
          HAVING    COUNT(1) > 1
        ) n
WHERE   m.C# = n.C#
        AND m.score = n.score
ORDER BY m.C# ,
        m.score ,
        m.S#
--方法2
SELECT  m.*
FROM    SC m
WHERE   EXISTS ( SELECT 1
                 FROM   ( SELECT    C# ,
                                    score
                          FROM      SC
                          GROUP BY  C# ,
                                    score
                          HAVING    COUNT(1) > 1
                        ) n
                 WHERE  m.C# = n.C#
                        AND m.score = n.score )
ORDER BY m.C# ,
        m.score ,
        m.S#

--42、查询每门功成绩最好的前两名
SELECT  t.*
FROM    sc t
WHERE   score IN ( SELECT TOP 2
                            score
                   FROM     sc
                   WHERE    C# = T.C#
                   ORDER BY score DESC )
ORDER BY t.C# ,
        t.score DESC

--43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT  Course.C# ,
        Course.Cname ,
        COUNT(*) [学生人数]
FROM    Course ,
        SC
WHERE   Course.C# = SC.C#
GROUP BY Course.C# ,
        Course.Cname
HAVING  COUNT(*) >= 5
ORDER BY [学生人数] DESC ,
        Course.C#

--44、检索至少选修两门课程的学生学号
SELECT  student.S# ,
        student.Sname
FROM    student ,
        SC
WHERE   student.S# = SC.S#
GROUP BY student.S# ,
        student.Sname
HAVING  COUNT(1) >= 2
ORDER BY student.S#

--45、查询选修了全部课程的学生信息
--方法1 根据数量来完成
SELECT  student.*
FROM    student
WHERE   S# IN ( SELECT  S#
                FROM    sc
                GROUP BY S#
                HAVING  COUNT(1) = ( SELECT COUNT(1)
                                     FROM   course
                                   ) )
--方法2 使用双重否定来完成
SELECT  t.*
FROM    student t
WHERE   t.S# NOT IN ( SELECT DISTINCT
                                m.S#
                      FROM      ( SELECT    S# ,
                                            C#
                                  FROM      student ,
                                            course
                                ) m
                      WHERE     NOT EXISTS ( SELECT 1
                                             FROM   sc n
                                             WHERE  n.S# = m.S#
                                                    AND n.C# = m.C# ) )
--方法3 使用双重否定来完成
SELECT  t.*
FROM    student t
WHERE   NOT EXISTS ( SELECT 1
                     FROM   ( SELECT DISTINCT
                                        m.S#
                              FROM      ( SELECT    S# ,
                                                    C#
                                          FROM      student ,
                                                    course
                                        ) m
                              WHERE     NOT EXISTS ( SELECT 1
                                                     FROM   sc n
                                                     WHERE  n.S# = m.S#
                                                            AND n.C# = m.C# )
                            ) k
                     WHERE  k.S# = t.S# )

--46、查询各学生的年龄
--46.1 只按照年份来算
SELECT  * ,
        DATEDIFF(yy, sage, GETDATE()) [年龄]
FROM    student
--46.2 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
SELECT  * ,
        CASE WHEN RIGHT(CONVERT(VARCHAR(10), GETDATE(), 120), 5) < RIGHT(CONVERT(VARCHAR(10), sage, 120),
                                                              5)
             THEN DATEDIFF(yy, sage, GETDATE()) - 1
             ELSE DATEDIFF(yy, sage, GETDATE())
        END [年龄]
FROM    student

--47、查询本周过生日的学生
SELECT  *
FROM    student
WHERE   DATEDIFF(week,
                 DATENAME(yy, GETDATE()) + RIGHT(CONVERT(VARCHAR(10), sage, 120),
                                                 6), GETDATE()) = 0

--48、查询下周过生日的学生
SELECT  *
FROM    student
WHERE   DATEDIFF(week,
                 DATENAME(yy, GETDATE()) + RIGHT(CONVERT(VARCHAR(10), sage, 120),
                                                 6), GETDATE()) = -1

--49、查询本月过生日的学生
SELECT  *
FROM    student
WHERE   DATEDIFF(mm,
                 DATENAME(yy, GETDATE()) + RIGHT(CONVERT(VARCHAR(10), sage, 120),
                                                 6), GETDATE()) = 0

--50、查询下月过生日的学生
SELECT  *
FROM    student
WHERE   DATEDIFF(mm,
                 DATENAME(yy, GETDATE()) + RIGHT(CONVERT(VARCHAR(10), sage, 120),
                                                 6), GETDATE()) = -1

DROP TABLE  Student,Course,Teacher,SC

 

 

 

展开阅读全文
打赏
0
7 收藏
分享
加载中
更多评论
打赏
0 评论
7 收藏
0
分享
在线直播报名
返回顶部
顶部