当前位置: 网学 > 编程文档 > SQL SERVER > 正文

50个常用sql语句

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/18
下载{$ArticleTitle}原创论文样式
D,课程名称,[100-85],[85-70],[70-60],[ <60]
    SELECT SC.C# as 课程ID, Cname as 课程名称
        ,SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100 - 85]
        ,SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85 - 70]
        ,SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [70 - 60]
        ,SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) AS [60 -]
    FROM SC,Course
    where SC.C#=Course.C#
    GROUP BY SC.C#,Cname;

24、查询学生平均成绩及其名次
      SELECT 1+(SELECT COUNT( distinct 平均成绩)
              FROM (SELECT S#,AVG(score) AS 平均成绩
                      FROM SC
                  GROUP BY S#
                  ) AS T1
            WHERE 平均成绩 > T2.平均成绩) as 名次,
      S# as 学生学号,平均成绩
    FROM (SELECT S#,AVG(score) 平均成绩
            FROM SC
        GROUP BY S#
        ) AS T2
    ORDER BY 平均成绩 desc;
 
25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
      SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数
      FROM SC t1
      WHERE score IN (SELECT TOP 3 score
              FROM SC
              WHERE t1.C#= C#
            ORDER BY score DESC
              )
      ORDER BY t1.C#;
26、查询每门课程被选修的学生数
  select c#,count(S#) from sc group by C#;
27、查询出只选修了一门课程的全部学生的学号和姓名
  select SC.S#,Student.Sname,count(C#) AS 选课数
  from SC ,Student
  where SC.S#=Student.S# group by SC.S# ,Student.Sname having count(C#)=1;
28、查询男生、女生人数
    Select count(Ssex) as 男生人数 from Student group by Ssex having Ssex=''男'';
    Select count(Ssex) as 女生人数 from Student group by Ssex having Ssex=''女'';
29、查询姓“张”的学生名单
    SELECT Sname FROM Student WHERE Sname like ''张%'';
30、查询同名同性学生名单,并统计同名人数
  sel

  • 上一篇资讯: SQL Server 连接基础知识
  • 网学推荐

    免费论文

    原创论文

    浏览:
    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
    湘ICP备09003080号