zoukankan      html  css  js  c++  java
  • SQL面试题——查询课程

    题目:  
    成绩表(Grade),包含字段:GradeID(Int,自增), SNO(int, 学号), CNO(int, 课程号), Score(float,分数)

        查询每门课程的平均(最高/最低)分及课程号;

        查询每门课程第1名的学生的学号;

        查询每门课程中超过平均分的所有学生的学号等等;

     

    解答:

    创建表

    Create Table Grade
    (
        GradeID int not null identity(0, 1)
            constraint PK_Grade primary key,
        SNO int,
        CNO int,
        Score float
    )
    Insert into dbo.Grade
    (SNO, CNO, Score)
    values
    (22,23,24),
    (32,33,44),
    (52,53,54),
    (22,23,25),
    (22,23,26),
    (22,23,27),
    (22,23,28),
    (22,23,29),
    (22,23,30);

    1.查询每门课程的平均(最高/最低)分及课程号:

    Select AVG(Score) as AvgScore, CNO from Grade group by CNO
    Select MAX(Score) as MaxScore, CNO From grade group by cno

    2.查询每门课程第1名的学生的学号:

    select * from Grade a where not exists(    
        select 1 from grade b where b.cno = a.cno and (b.score < a.score 
             or (b.score = a.score and gradeid < a.gradeid)))

    3.查询每门课程中超过平均分的所有学生的学号:

    select * from grade a  where not exists(
        select * from(select AVG(score) avgScore, cno from grade group by cno) b  where a.cno = b.cno and a.score < b.avgScore)
  • 相关阅读:
    iscroll4实现轮播图效果
    用css样式围剿等高列问题(转载)
    一些小bug
    前端代码规范
    !DOCTYPE html文档类型声明简写 HTML5 DOCTYPE缩写
    JS判断鼠标向上滚动还是向下滚动
    css Cursor:url()自定义鼠标指针样式为图片
    线段树-矩形面积求并
    值域线段树 bzoj 4627
    简单数位DP
  • 原文地址:https://www.cnblogs.com/hellolong/p/4065447.html
Copyright © 2011-2022 走看看