1. -----------聚合函数使用------------------------
  2.  
  3. --1、查询student表中所有学生人数
  4. select count(stuno) from student
  5.  
  6. --2、查询stucou表中选课的人次
  7. select count(*)as 选课人数 from stucou
  8.  
  9. --3、查询stucou表中学生所选课程数量
  10. select count(distinct couno) from stucou
  11.  
  12. --4、查询stucou表中选了001课程的人数
  13. select count(*) from stucou where couno=''
  14.  
  15. --5、查询stucou表中第2志愿(willorder)选了001课程的人数
  16. select count(*) from stucou where willorder='' and couno=''
  17.  
  18. --6、统计student2010表中籍贯与你相同(同一县、区或市)的学生人数
  19. select count(*) from student2010 where jtdz like '%汕头%'
  20.  
  21. --7、统计student2010表中与你同姓的学生人数
  22. select * from student2010 where xm like '陈%'
  23.  
  24. --8、查询qypt08class表班级最多的人数
  25. select max(rs) from qypt08class
  26.  
  27. --9、查询qypt08class表护理学院的班级最少人数
  28. select min(rs) from qypt08class
  29.  
  30. ---------分组统计(group by子句使用)--------------------
  31.  
  32. --1、统计student2010表中男、女生人数
  33. select xb, count(xb) from student2010 group by xb
  34.  
  35. --2、统计stucou表中各门课程的选修人数
  36. select * from stucou
  37. select couno, count(*) from stucou group by couno
  38.  
  39. --3、统计stucou表中每个学生选修的课程数量
  40. select * from stucou
  41. select stuno,count(*) from stucou group by stuno
  42.  
  43. --4、统计student2010表中每个院系的学生人数
  44. select * from student2010
  45. select xymc,count(*) from student2010 group by xymc
  46. --5、统计student2010表中每个班的学生人数,显示yxmc,bj及对应的人数,并按人数由多到少排序
  47. select * from student2010
  48. select bjmc,xymc,count(*) as 人数 from student2010 group by bjmc,xymc order by 人数 desc
  49. --6、统计student2010表中各民族学生人数,并按人数由少到多排序
  50. select mz,count(*) from student2010 group by mz order by count(*)
  51.  
  52. --7、在student2010表分专业统计男、女生人数,按专业名称排序
  53. select zymc,xb,count(*) as 人数 from student2010 group by zymc,xb order by 人数 desc
  54.  
  55. -------------------对分组统计的结果进一步筛选(having子句使用)------------------------------
  56.  
  57. --1、查询qypt08class表中各院系的人数,只显示人数多于400的记录
  58. select * from qypt08class
  59. select yx,sum(rs) from qypt08class group by yx having sum(rs)>400
  60. --2、统计stucou表中各门课程的选修人数,只显示人数少于30的记录(显示couno及对应的人数)
  61. select * from stucou
  62. select couno,count(*) from stucou group by couno having count(*)<30
  63. --3、查询student2010表中人数多于70人的班级的xymczymcbjmcrs(人数)
  64. select * from student2010
  65. select xymc,zymc,bjmc,count(*) from student2010 group by xymc,zymc,bjmc having count(*)>20
  66. --------------------coupute子句使用----------------------
  67.  
  68. --1、在qypt08class中统计每个院系人数多于60的班级数,并显示统计的明确
  69.  
  70. -------------------------将查询保存为新表(into)--------------------
  71.  
  72. --1、查询student2010表的xymczymcbjmcxhxm五个字段内容,并将查询结果保存到新表student2010A
  73.  
  74. --查询表student2010A的内容,检验上题操作结果
  75.  
  76. --2、统计student2010表中每班的人数(rs),并将结果保存到新表class2010,新表包含xymczymcbjmcrs四个字段
  77.  
  78. --查询表class2010的内容,检验上题操作结果
  79.  
  80. --3、查询表student2011中所有女生的信息,并将结果保存到表girl2011
  81.  
  82. ------使用嵌套子查询完成1-7题----------
  83.  
  84. --1、在qypt08student表中查询和“陈小梅”在同一班级的所有男同学的信息。
  85. select * from qypt08student where bjmc in (select bjmc from qypt08student where xm='陈小梅') and xb='男'
  86.  
  87. --2、在qypt08student表中查询和“黄巧”在同一院系的所有女同学的信息。
  88. select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女'
  89.  
  90. --3、在qypt08student表中查询和“黄巧”在同一院系的所有陈姓女同学的信息。
  91. select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女' and xm like '陈%'
  92. --4、查询course表中最多人选修的课程信息(willnum最大)
  93. select * from course where willnum in (select max(willnum) from course)
  94.  
  95. --5、查询course表中最少人选修的课程信息(willnum最小)
  96. select * from course where willnum=(select min(willnum) from course)
  97.  
  98. --6、查询course表中选修人数大于平均选修数的课程信息
  99. select * from course
  100. select from course
  101. --7、查询course表中选修人数少于平均选修数的课程信息
  102.  
  103. ------使用相关子查询完成以下题目----------
  104.  
  105. --8、查询所有有选修课的学生信息
  106.  
  107. --9、查询没有选修课程的学生信息
  108.  
  109. --10、查询没有人选修的课程信息
  110.  
  111. --11、查找选修了课程号为002的课程的学生信息
  112.  
  113. --12、查找20000001班没有选修课程号为004的课程的学生信息
  114.  
  115. --13、查找选修了“智能建筑”课程的学生信息

  1. ------使用嵌套子查询完成1-7题----------
  2.  
  3. --1、在qypt08student表中查询和“陈小梅”在同一班级的所有男同学的信息。
  4. select * from qypt08student where bjmc in (select bjmc from qypt08student where xm='陈小梅') and xb='男'
  5.  
  6. --2、在qypt08student表中查询和“黄巧”在同一院系的所有女同学的信息。
  7. select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女'
  8.  
  9. --3、在qypt08student表中查询和“黄巧”在同一院系的所有陈姓女同学的信息。
  10. select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女' and xm like '陈%'
  11. --4、查询course表中最多人选修的课程信息(willnum最大)
  12. select * from course where willnum in (select max(willnum) from course)
  13.  
  14. --5、查询course表中最少人选修的课程信息(willnum最小)
  15. select * from course where willnum=(select min(willnum) from course)
  16.  
  17. --6、查询course表中选修人数大于平均选修数的课程信息
  18. select * from course
  19. select from course
  20. --7、查询course表中选修人数少于平均选修数的课程信息
  21.  
  22. ------使用相关子查询完成以下题目----------
  23.  
  24. --8、查询所有有选修课的学生信息
  25.  
  26. --9、查询没有选修课程的学生信息
  27.  
  28. --10、查询没有人选修的课程信息
  29.  
  30. --11、查找选修了课程号为002的课程的学生信息
  31.  
  32. --12、查找20000001班没有选修课程号为004的课程的学生信息
  33.  
  34. --13、查找选修了“智能建筑”课程的学生信息

  1. --1、在qypt08student表中查询和“陈小梅”在同一班级的所有男同学的信息。
  2. select * from qypt08student where bjmc in (select bjmc from qypt08student where xm='陈小梅') and xb='男'
  3.  
  4. --2、在qypt08student表中查询和“黄巧”在同一院系的所有女同学的信息。
  5.  
  6. select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女'
  7. --3、在qypt08student表中查询和“黄巧”在同一院系的所有陈姓女同学的信息。
  8.  
  9. select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女' and xm like '陈%'
  10. --4、查询course表中最多人选修的课程信息(willnum最大)
  11. select * from course where willnum in (select max(willnum) from course)
  12.  
  13. --5、查询course表中最少人选修的课程信息(willnum最小)
  14.  
  15. select * from course where willnum=(select min(willnum) from course)
  16.  
  17. --6、查询course表中选修人数大于平均选修数的课程信息
  18. select avg(willnum) from course
  19. select * from course where willnum > (select avg(willnum) from course )
  20.  
  21. --7、查询course表中选修人数少于平均选修数的课程信息
  22. select * from course where willnum < (select avg(willnum) from course )
  23.  
  24. --8、查询所有有选修课的学生信息
  25. select * from student
  26. select * from course
  27. select distinct stuno from stucou
  28. select * from student where stuno in (select distinct stuno from stucou)
  29. --9、查询没有选修课程的学生信息
  30. select * from student where stuno not in (select distinct stuno from stucou)
  31.  
  32. --10、查询没有人选修的课程信息
  33. select * from course where willnum =''
  34.  
  35. --11、查找选修了课程号为002的课程的学生信息
  36. select stuno from stucou where couno =''
  37. select * from student where stuno in (select stuno from stucou where couno ='')
  38. --12、查找20000001班没有选修课程号为004的课程的学生信息
  39. select * from class where classno =''
  40. select * from course where couno not =''
  41.  
  42. --13、查找选修了“智能建筑”课程的学生信息
  43.  
  44. --14、查询成绩表中大于平均分的学生信息
  45.  
  46. --15、查询已经选修了课程的学生信息
  47.  
  48. --视图练习
  49. --------------------------------------------------------------------------------
  50. --第一题
  51. --1、使用企业管理器创建视图,要求数据表的来源为:department,class,student三个表
  52. -----显示学生每个学生所属的院系名称、班级名称、学号及姓名,视图保存为v_student
  53.  
  54. --2、在查询分析器中查看视图V_student的数据
  55.  
  56. --3、使用T-SQL语句创建一视图,要求数据表的来源为:department,class,student三个表
  57. -----显示学生每个学生所属的院系名称、班级名称、学号及姓名,视图保存为v_student2
  58.  
  59. --4、在查询分析器中查看视图V_student2的数据
  60.  
  61. --第二题
  62. --1、使用企业管理器创建视图,数据表的来源为:class,student,course,stucou四个表
  63. -----显示每个学生的班级名称、学号、选修的课程名称,视图保存为v_cou
  64.  
  65. --2、在查询分析器中查看视图V_cou的数据
  66.  
  67. --3、使用T-SQL语句创建一视图,数据表的来源为:class,student,course,stucou四个表
  68. -----显示每个学生的班级名称、学号、选修的课程名称,视图保存为v_cou2
  69.  
  70. --4、在查询分析器中查看视图V_cou2的数据
  71.  
  72. --第三题
  73. --1、使用企业管理器创建视图,数据表的来源为:department,class,student,course,stucou五个表
  74. -----显示每个学生所属系部名称,班级名称、学号、姓名、选修的课程名称,视图保存为v_cou2A
  75.  
  76. --2、在查询分析器中查看视图V_cou2A的数据
  77.  
  78. --3、使用T-SQL语句创建一视图,数据表的来源为:department,class,student,course,stucou五个表
  79. -----显示每个学生所属系部名称,班级名称、学号、姓名、选修的课程名称,视图保存为v_cou2B
  80.  
  81. --4、在查询分析器中查看视图V_cou2B的数据
  82.  
  83. --第四题
  84. --1、使用T-SQL语句创建一视图,命名为V_stunocou。要求数据表的来源为stucou,course两个表
  85. -----显示学生的学号及所选课程的名称,并加密视图的定义
  86.  
  87. --2、在查询分析器中查看视图V_stunocou的数据

  1. --1、检索student2010表中学制(XZ)为2年的学生信息
  2.  
  3. --2、检索student2010表中班级名称(BJMC)为“2010计算机网络技术1班”的学生信息
  4.  
  5. --3、检索student2010表中专业名称(ZYMC)为“计算机网络技术”的学生信息,按姓氏排序显示
  6.  
  7. --4、检索student2010表中专业名称(ZYMC)为“计算机网络技术”的学生的学号、姓名字段,字段名用中文显示
  8.  
  9. --5、检索stucou表中选修了004009010015课程的记录
  10.  
  11. --6、检索student2010表中姓名最后一个字为“华”的女学生信息
  12.  
  13. --7、检索student2010表中清新籍学生的信息
  14.  
  15. --8、显示qypt08student表中的系部名称(不重复显示)
  16.  
  17. --9、显示stucou表中所有willorder1的记录
  18.  
  19. --10、显示stucou表中所有couno003的记录
  20.  
  21. --11、显示stucou表中所有willorder1couno003的记录
  22.  
  23. --12、显示stucou表中所有willorder24的记录
  24.  
  25. --13、显示qypt08class表中备注(bz)不为空的记录
  26.  
  27. --14、显示qypt08student表中所有学号末位为1的记录
  28. select * from qypt08student where xh like '%1'
  29. --15、显示qypt08student表中每个班的学号为1号的记录
  30. select * from qypt08student where xh like '%01'
  31. --16、显示qypt08student表中所有姓‘张’的记录
  32. select * from qypt08student where xm like '张%'
  33. --17、显示qypt08student表中所有姓‘张’且姓名只包含两个字的记录
  34. select * from qypt08student where xm like '张_'
  35. --18、显示qypt08student表中所有姓‘张’且姓名只包含两个字的女性记录
  36. select * from qypt08student where xm like '张_' and xb like '女'
  37. --19、显示student表中Pwd的首末两位均为7的记录
  38. select * from student where pwd like '7%7'
  39. --20、显示qypt08student表中所有姓‘张’且姓名只包含三个字的记录
  40. select * from qypt08student where xm like '张%' and xm not like '张_'
  41. --21、显示qypt08student表中所有姓‘张’且姓名只包含三个字的男性记录
  42. select * from qypt08student where xm like '张%' and xm not like '张_' and xb like '男'
  43. --22、显示qypt08student表中所有姓张、李、刘的记录
  44. select * from qypt08student where xm like '[张,李,刘]%'
  45. --23、检索student2010表中身份证号码(SFZH)的最后一位为数字的学生信息
  46. select * from student2010 where sfzh like '%[0-9]'

SQL基础三(例子)的更多相关文章

  1. SQL基础三

    一.SQL ORDER BY 子句 ORDER BY 语句用于对结果集进行排序,默认按照升序对记录进行排序,如果需要按照降序进行排序,需要在后面追加关键字DESC.应用如下: 原始的表:Orders表 ...

  2. 数据库学习---SQL基础(二)

    数据库学习---SQL基础(一) 数据库学习---SQL基础(二) 数据库学习---SQL基础(三) 上篇复习的sql的增删改查,and ,or ,>=, <=,!=等逻辑运算符,还有in ...

  3. 数据库学习---SQL基础(一)

     数据库学习---SQL基础(一) 数据库学习---SQL基础(二) 数据库学习---SQL基础(三) SQL(struct query language)结构化查询语言:一种专门与数据库通信的语言, ...

  4. Oracle知识梳理(三)操作篇:SQL基础操作汇总

    Oracle知识梳理(三)操作篇:SQL基础操作汇总 一.表操作 1.表的创建(CREATE TABLE): 基本语句格式:       CREATE TABLE  table_name ( col_ ...

  5. 数据库整理(三) SQL基础

    数据库整理(三) SQL基础 SQL语言的特点 集数据定义语言(DDL),数据操纵语言(DML),数据控制语言(DCL)功能于一体. 可以独立完成数据库生命周期中的全部活动: ​ ●定义和修改.删除关 ...

  6. [SQL] SQL 基础知识梳理(三) - 聚合和排序

    SQL 基础知识梳理(三) - 聚合和排序 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5926689.html 序 这是<SQL 基础知识梳理 ...

  7. ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段

    ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...

  8. 第三章 - SQL基础及元数据获取

    SQL的介绍 SQL的定义:结构化查询语句 SQL的作用:对库和表进行操作 SQL的常用分类 DDL 数据定义语言(Data Definition Language) DCL 数据控制语言(Data ...

  9. [SQL] SQL 基础知识梳理(七)- 集合运算

    SQL 基础知识梳理(七)- 集合运算 目录 表的加减法 联结(以列为单位) 一.表的加减法 1.集合:记录的集合(表.视图和查询的执行结果). 2.UNION(并集):表的加法 -- DDL:创建表 ...

随机推荐

  1. [CF911F]Tree Destruction

    题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 首先我们知道,到一个点距离最远的点是直径的端点.考虑贪心,如果我 ...

  2. [BZOJ1217]消防局的设立

    Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来 连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成 ...

  3. webform CustomValidator

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.customvalidator?view=netframew ...

  4. [异常记录(二)] 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey> 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate。

    错误提示: 验证视图状态 MAC 失败.如果此应用程序由网络场或群集承载,请确保 <machineKey> 配置指定了相同的 validationKey 和验证算法.不能在群集中使用 Au ...

  5. js事件在不同浏览器之间的差异

    目录: 1. 介绍 2. 不同浏览器之间的差异 2.1 添加事件的方法 2.2 事件对象event 2.3 event中的属性/方法 3. 总结 1. 介绍 javascript与HTML之间的交互是 ...

  6. char数组

    *****************************************************************char 类型的数组(c语言中是没有字符串的)char name[20 ...

  7. django 使用form组件提交数据之form表单提交

    django的form组件可以减少后台在进行一些重复性的验证工作,极大降低开发效率. 最近遇到一个问题: 当使用form表单提交数据后,如果数据格式不符合后台定义的规则,需要重新在前端页面填写数据. ...

  8. Apache 2 移植到Arm开发板

    第一步,安装pcre: tar -xvzf pcre-8.31.tar.gz cd pcre-8.31 ./configure --prefix=$ARMROOTFS/usr/pcre 的错误,如下图 ...

  9. Dubbo 和 Spring Cloud微服务架构 比较及相关差异

    你真的了解微服务架构吗?听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构. 微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务, ...

  10. 输入T,返回TResult的委托

    下面的 委托 兼容输入 参数T,并且 返回值类型为TResult 的 方法(即封装一个具有一个参数并返回TResult 参数指定的类型值的方法) public delegate TResult Fun ...