SQL基础三(例子)
-----------聚合函数使用--------------------------1、查询student表中所有学生人数 select count(stuno) from student--2、查询stucou表中选课的人次 select count(*)as 选课人数 from stucou--3、查询stucou表中学生所选课程数量 select count(distinct couno) from stucou --4、查询stucou表中选了001课程的人数 select count(*) from stucou where couno='001'--5、查询stucou表中第2志愿(willorder)选了001课程的人数 select count(*) from stucou where willorder='2' and couno='001'--6、统计student2010表中籍贯与你相同(同一县、区或市)的学生人数 select count(*) from student2010 where jtdz like '%汕头%'--7、统计student2010表中与你同姓的学生人数 select * from student2010 where xm like '陈%'--8、查询qypt08class表班级最多的人数 select max(rs) from qypt08class--9、查询qypt08class表护理学院的班级最少人数 select min(rs) from qypt08class---------分组统计(group by子句使用)----------------------1、统计student2010表中男、女生人数 select xb, count(xb) from student2010 group by xb--2、统计stucou表中各门课程的选修人数 select * from stucou select couno, count(*) from stucou group by couno--3、统计stucou表中每个学生选修的课程数量 select * from stucou select stuno,count(*) from stucou group by stuno--4、统计student2010表中每个院系的学生人数 select * from student2010 select xymc,count(*) from student2010 group by xymc --5、统计student2010表中每个班的学生人数,显示yxmc,bj及对应的人数,并按人数由多到少排序 select * from student2010 select bjmc,xymc,count(*) as 人数 from student2010 group by bjmc,xymc order by 人数 desc --6、统计student2010表中各民族学生人数,并按人数由少到多排序 select mz,count(*) from student2010 group by mz order by count(*)--7、在student2010表分专业统计男、女生人数,按专业名称排序 select zymc,xb,count(*) as 人数 from student2010 group by zymc,xb order by 人数 desc-------------------对分组统计的结果进一步筛选(having子句使用)--------------------------------1、查询qypt08class表中各院系的人数,只显示人数多于400的记录 select * from qypt08class select yx,sum(rs) from qypt08class group by yx having sum(rs)>400 --2、统计stucou表中各门课程的选修人数,只显示人数少于30的记录(显示couno及对应的人数) select * from stucou select couno,count(*) from stucou group by couno having count(*)<30 --3、查询student2010表中人数多于70人的班级的xymc、zymc、bjmc及rs(人数) select * from student2010 select xymc,zymc,bjmc,count(*) from student2010 group by xymc,zymc,bjmc having count(*)>20 --------------------coupute子句使用------------------------1、在qypt08class中统计每个院系人数多于60的班级数,并显示统计的明确-------------------------将查询保存为新表(into)----------------------1、查询student2010表的xymc、zymc、bjmc、xh、xm五个字段内容,并将查询结果保存到新表student2010A中--查询表student2010A的内容,检验上题操作结果--2、统计student2010表中每班的人数(rs),并将结果保存到新表class2010,新表包含xymc、zymc、bjmc、rs四个字段--查询表class2010的内容,检验上题操作结果--3、查询表student2011中所有女生的信息,并将结果保存到表girl2011中------使用嵌套子查询完成1-7题------------1、在qypt08student表中查询和“陈小梅”在同一班级的所有男同学的信息。 select * from qypt08student where bjmc in (select bjmc from qypt08student where xm='陈小梅') and xb='男'--2、在qypt08student表中查询和“黄巧”在同一院系的所有女同学的信息。 select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女'--3、在qypt08student表中查询和“黄巧”在同一院系的所有陈姓女同学的信息。 select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女' and xm like '陈%' --4、查询course表中最多人选修的课程信息(willnum最大) select * from course where willnum in (select max(willnum) from course)--5、查询course表中最少人选修的课程信息(willnum最小) select * from course where willnum=(select min(willnum) from course)--6、查询course表中选修人数大于平均选修数的课程信息 select * from course select from course --7、查询course表中选修人数少于平均选修数的课程信息------使用相关子查询完成以下题目------------8、查询所有有选修课的学生信息--9、查询没有选修课程的学生信息--10、查询没有人选修的课程信息--11、查找选修了课程号为002的课程的学生信息--12、查找20000001班没有选修课程号为004的课程的学生信息--13、查找选修了“智能建筑”课程的学生信息
------使用嵌套子查询完成1-7题------------1、在qypt08student表中查询和“陈小梅”在同一班级的所有男同学的信息。
select * from qypt08student where bjmc in (select bjmc from qypt08student where xm='陈小梅') and xb='男'--2、在qypt08student表中查询和“黄巧”在同一院系的所有女同学的信息。
select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女'--3、在qypt08student表中查询和“黄巧”在同一院系的所有陈姓女同学的信息。
select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女' and xm like '陈%'
--4、查询course表中最多人选修的课程信息(willnum最大)
select * from course where willnum in (select max(willnum) from course)--5、查询course表中最少人选修的课程信息(willnum最小)
select * from course where willnum=(select min(willnum) from course)--6、查询course表中选修人数大于平均选修数的课程信息
select * from course
select from course
--7、查询course表中选修人数少于平均选修数的课程信息------使用相关子查询完成以下题目------------8、查询所有有选修课的学生信息--9、查询没有选修课程的学生信息--10、查询没有人选修的课程信息--11、查找选修了课程号为002的课程的学生信息--12、查找20000001班没有选修课程号为004的课程的学生信息--13、查找选修了“智能建筑”课程的学生信息
--1、在qypt08student表中查询和“陈小梅”在同一班级的所有男同学的信息。 select * from qypt08student where bjmc in (select bjmc from qypt08student where xm='陈小梅') and xb='男'--2、在qypt08student表中查询和“黄巧”在同一院系的所有女同学的信息。select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女' --3、在qypt08student表中查询和“黄巧”在同一院系的所有陈姓女同学的信息。select * from qypt08student where yx=(select yx from qypt08student where xm='黄巧' ) and xb='女' and xm like '陈%' --4、查询course表中最多人选修的课程信息(willnum最大) select * from course where willnum in (select max(willnum) from course)--5、查询course表中最少人选修的课程信息(willnum最小)select * from course where willnum=(select min(willnum) from course)--6、查询course表中选修人数大于平均选修数的课程信息 select avg(willnum) from course select * from course where willnum > (select avg(willnum) from course )--7、查询course表中选修人数少于平均选修数的课程信息 select * from course where willnum < (select avg(willnum) from course )--8、查询所有有选修课的学生信息 select * from student select * from course select distinct stuno from stucou select * from student where stuno in (select distinct stuno from stucou) --9、查询没有选修课程的学生信息 select * from student where stuno not in (select distinct stuno from stucou)--10、查询没有人选修的课程信息 select * from course where willnum ='0'--11、查找选修了课程号为002的课程的学生信息 select stuno from stucou where couno ='002' select * from student where stuno in (select stuno from stucou where couno ='002') --12、查找20000001班没有选修课程号为004的课程的学生信息select * from class where classno ='20000001' select * from course where couno not ='004'--13、查找选修了“智能建筑”课程的学生信息--14、查询成绩表中大于平均分的学生信息--15、查询已经选修了课程的学生信息--视图练习 -------------------------------------------------------------------------------- --第一题 --1、使用企业管理器创建视图,要求数据表的来源为:department,class,student三个表 -----显示学生每个学生所属的院系名称、班级名称、学号及姓名,视图保存为v_student--2、在查询分析器中查看视图V_student的数据--3、使用T-SQL语句创建一视图,要求数据表的来源为:department,class,student三个表 -----显示学生每个学生所属的院系名称、班级名称、学号及姓名,视图保存为v_student2--4、在查询分析器中查看视图V_student2的数据--第二题 --1、使用企业管理器创建视图,数据表的来源为:class,student,course,stucou四个表 -----显示每个学生的班级名称、学号、选修的课程名称,视图保存为v_cou--2、在查询分析器中查看视图V_cou的数据--3、使用T-SQL语句创建一视图,数据表的来源为:class,student,course,stucou四个表 -----显示每个学生的班级名称、学号、选修的课程名称,视图保存为v_cou2--4、在查询分析器中查看视图V_cou2的数据--第三题 --1、使用企业管理器创建视图,数据表的来源为:department,class,student,course,stucou五个表 -----显示每个学生所属系部名称,班级名称、学号、姓名、选修的课程名称,视图保存为v_cou2A--2、在查询分析器中查看视图V_cou2A的数据--3、使用T-SQL语句创建一视图,数据表的来源为:department,class,student,course,stucou五个表 -----显示每个学生所属系部名称,班级名称、学号、姓名、选修的课程名称,视图保存为v_cou2B--4、在查询分析器中查看视图V_cou2B的数据--第四题 --1、使用T-SQL语句创建一视图,命名为V_stunocou。要求数据表的来源为stucou,course两个表 -----显示学生的学号及所选课程的名称,并加密视图的定义--2、在查询分析器中查看视图V_stunocou的数据
--1、检索student2010表中学制(XZ)为2年的学生信息--2、检索student2010表中班级名称(BJMC)为“2010计算机网络技术1班”的学生信息--3、检索student2010表中专业名称(ZYMC)为“计算机网络技术”的学生信息,按姓氏排序显示--4、检索student2010表中专业名称(ZYMC)为“计算机网络技术”的学生的学号、姓名字段,字段名用中文显示--5、检索stucou表中选修了004、009、010及015课程的记录--6、检索student2010表中姓名最后一个字为“华”的女学生信息--7、检索student2010表中清新籍学生的信息--8、显示qypt08student表中的系部名称(不重复显示)--9、显示stucou表中所有willorder为1的记录--10、显示stucou表中所有couno为003的记录--11、显示stucou表中所有willorder为1且couno为003的记录--12、显示stucou表中所有willorder为2到4的记录--13、显示qypt08class表中备注(bz)不为空的记录--14、显示qypt08student表中所有学号末位为1的记录
select * from qypt08student where xh like '%1'
--15、显示qypt08student表中每个班的学号为1号的记录
select * from qypt08student where xh like '%01'
--16、显示qypt08student表中所有姓‘张’的记录
select * from qypt08student where xm like '张%'
--17、显示qypt08student表中所有姓‘张’且姓名只包含两个字的记录
select * from qypt08student where xm like '张_'
--18、显示qypt08student表中所有姓‘张’且姓名只包含两个字的女性记录
select * from qypt08student where xm like '张_' and xb like '女'
--19、显示student表中Pwd的首末两位均为7的记录
select * from student where pwd like '7%7'
--20、显示qypt08student表中所有姓‘张’且姓名只包含三个字的记录
select * from qypt08student where xm like '张%' and xm not like '张_'
--21、显示qypt08student表中所有姓‘张’且姓名只包含三个字的男性记录
select * from qypt08student where xm like '张%' and xm not like '张_' and xb like '男'
--22、显示qypt08student表中所有姓张、李、刘的记录
select * from qypt08student where xm like '[张,李,刘]%'
--23、检索student2010表中身份证号码(SFZH)的最后一位为数字的学生信息
select * from student2010 where sfzh like '%[0-9]'
转载于:https://www.cnblogs.com/tcheng/p/5959073.html
SQL基础三(例子)相关推荐
- [SQL] SQL 基础知识梳理(三) - 聚合和排序
SQL 基础知识梳理(三) - 聚合和排序 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5926689.html 序 这是<SQL 基础知识梳理 ...
- SQL基础教程MICK版 ···第三章总结
SQL基础教程MICK版 ···第三章总结 SELECT语句 关于上表的语句顺序 和 执行顺序 完整的语法结构 GROUP BY需要注意的点 用于限定GROUP BY的 HAVING子句 关于ORDE ...
- sql数据库三个重点复习的基础语句(游标,索引,视图)
(l)SQL基础语句 1.创建数据库 CREATE DATABASE database-name 2.删除数据库 drop database dbname 4.创建新表 create table ta ...
- sql基础教程mysql_SQL基础教程(第2版)笔记整理
花了一段时间把SQL基础教程(第2版)看完,并把笔记整理好. 数据定义语言(Data Define Language) 数据操作语言(Data Manipulation Language) 数据控制语 ...
- (2.13)Mysql之SQL基础——触发器
(2.13)Mysql之SQL基础--触发器 关键词:Mysql触发器 二.触发器 MySQL语句在需要时被执行,存储过程也是如此,如果希望某条语句(或某些语句)在事件发生时自动执行,这就需要用到触发 ...
- SQL基础操作_5_字符串处理
目录 7.6 处理字符串 7.6.1 生成自增值 7.6.2 遍历字符串里的每个值 7.6.3 处理含引号的字符串 7.6.4 计算某个字符出现的次数 7.6.5 字符串里过滤不需要的字符 7.6.6 ...
- SQL基础操作_4_表的插入、更新、删除、合并操作
目录 表的插入.更新.删除.合并操作 7.4.1 插入新的记录 7.4.2 插入含自增列的记录 7.4.3 插入新的多条记录 7.4.4 同时往多个表插入记录 7.4.5 通过其它表插入 7.4.6 ...
- 《MySQL DBA修炼之道》——3.3 SQL基础
本节书摘来自华章出版社<MySQL DBA修炼之道>一书中的第3章,第3.3节,作者:陈晓勇,更多章节内容可以访问云栖社区"华章计算机"公众号查看 3.3 SQL基础 ...
- 20个案例掌握PL/SQL 基础
有MS SQL基础,学习了两周多的PL/SQL,做了一些事例,但是很多信息在网上难以找到太多正确的答案,看到一篇又一篇的PL/SQL博文,案例方面的博文一篇又一篇的雷同,一看就是是Ctrl+C的复制. ...
最新文章
- java 实现 excel sheet 拷贝到另一个Excel文件中 poi
- SimpleUrlHandlerMapping 处理器映射的配置--转
- ArcGIS API for JavaScript压缩版(compact)与标准版的区别
- 用C#创建COM组件全过程
- lib和dll的区别、生成以及使用详解
- Linux 内核态与用户态通信 netlink
- potplay显示服务器关闭,PotPlayer怎么关掉左上角显示的播放时间?PotPlayer关掉左上角显示播放时间的操作步骤...
- 将自己的dcm数据制作成LUNA16数据集提供数据样式之代码整理
- Linux系统管理(6)——Linux下启动Redis服务的几种方法
- java+c#+json+时间_Java与C#间json日期格式互转完美解决方案
- Nginx1.10编译安装
- 神奇的G1——Java全新垃圾回收机制
- 苹果Mac最灵活的文件共享工具:​​​​Dropshare
- Nvidia驱动支持的linux版本,完善支持 NVIDIA显卡Linux驱动275.19正式版
- Quartz 视频教程免费下载
- Configured.java
- 清理注册表 php,怎样清理注册表?
- 小程序开发工具_小程序开发工具都有哪些?
- 无法通过 Internet 连接到 Visual Paradigm 的服务器
- LabVIEW崩溃后如何排查故障
热门文章
- Some Essential JavaScript Questions And Answers(5)
- postman返回值设置为全局变量
- 我的 atom 开发工具
- 亲测能用的mysqli类,挺好用的
- Nested Loop,Sort Merge Join,Hash Join
- Dll 导出类 [示例代码]
- 2011.10.16
- spark-shell连接数据库java.sql.SQLSyntaxErrorException: Unknown databas
- armadillo 使用注意 越界不报错
- 大杂烩, 硬盘安装Linux