数据库函数练习
2016-06-11 14:17
204 查看
--创建数据库 CREATE database xinxi2 go --应用这个数据库 use xinxi2 go --创建表格 create table xs ( code int, name varchar(20), sex char(10), nianling decimal(18,2), tizhong decimal(18,2), yuwen decimal(18,2), shuxue decimal(18,2), yingyu decimal(18,2), banji varchar(20) ) go --插入数据 insert into xs values(1,'张三里','男',22,70,50,70,80,'一班') insert into xs values(2,'王晓丽','女',23,55,85,70,90,'一班') insert into xs values(3,'李四','男',25,75,60,80,55,'一班') insert into xs values(4,'刘小红','女',22,56,70,60,80,'一班') insert into xs values(5,'王五','男',26,72,60,58,66,'一班') insert into xs values(6,'王武胜','男',25,65,82,73,77,'一班') insert into xs values(7,'赵玉梅','女',20,50,82,55,65,'一班') insert into xs values(8,'马三全','男',20,70,55,60,70,'一班') insert into xs values(9,'李玉兰','女',25,52,66,78,68,'一班') insert into xs values(10,'石青志','男',25,75,80,85,78,'一班') insert into xs values(11,'李志','男',21,65,90,95,90,'二班') insert into xs values(12,'胡丽','女',23,50,75,70,80,'二班') insert into xs values(13,'赵六','男',27,75,80,60,58,'二班') insert into xs values(14,'严春香','女',23,50,80,80,80,'二班') insert into xs values(15,'齐秦','男',29,70,80,50,88,'二班') insert into xs values(16,'韩明鑫','男',26,68,65,50,40,'二班') insert into xs values(17,'刘亦菲','女',25,48,85,89,90,'二班') insert into xs values(18,'黄晓明','男',29,75,80,50,60,'二班') insert into xs values(19,'朱莉','女',20,50,88,70,60,'二班') insert into xs values(20,'尼古拉斯','男',26,70,80,70,88,'二班') go --查看表格 select * from xs --SUM 练习 --求总体重 select SUM(tizhong) as 总体重 from xs --求语文的总分数 select SUM(yuwen) as 语文总成绩 from xs --求数学总分数 select SUM(shuxue) as 数学总成绩 from xs --求英语总成绩 select SUM(yingyu) as 英语总成绩 from xs --求总成绩 --AVG 练习 --求平均体重 select AVG(tizhong) as 平均体重 from xs --求女生,男生的平均体重 select AVG(tizhong) as 女生平均体重 from xs where sex='女' select AVG(tizhong) as 男生平均体重 from xs where sex='男' --求语文平均分 select AVG(yuwen) as 语文平均成绩 from xs --求数学的平均分 select AVG(shuxue) as 数学平均成绩 from xs --求数学的平均分 select AVG(yingyu) as 英语平均成绩 from xs --求一班英语平均成绩 select AVG(yingyu) as 一班英语成绩 from xs where banji='一班' --求二班数学平均成绩 select AVG(shuxue) as 二班数学平均成绩 from xs where banji='二班' --count 练习 --表格内部信息个数 select COUNT(*) from xs --女生有多少个 select COUNT(*) as 女生人数 from xs where sex='女' --男生个数 select COUNT(*) as 男生人数 from xs where sex='男' --一班男生数量 select COUNT(*) as 一班男生数量 from xs where sex='男' and banji='一班' --求姓刘的人数 select COUNT(*) as 刘姓人员个数 from xs where name like '刘%' --去重后显示个数 select COUNT(distinct banji) from xs --查询所有姓刘的同学 select * from xs where name like '刘%' --查询所有带有丽字的同学 select *from xs where name like '%丽' --排序 --体重升序排列 select * from xs order by tizhong asc --语文成绩升序排序 select* from xs order by yuwen asc --语文成绩降序排列 select * from xs order by yuwen desc --查看体重最重的三个人 select top 3 *from xs order by tizhong desc --查看英语前三名 select top 3 * from xs order by yingyu desc --查询二班数学前三名 select top 3 *from xs where banji='二班' order by shuxue desc --max 应用 --查询语文最高成绩 select MAX(yuwen) from xs --查询语文最低成绩 select min(yuwen) from xs --查询一班英语最高成绩 select MAX(yingyu) from xs where banji='一班' --group by --性别分组 select sex from xs group by sex --班级分组 select banji from xs group by banji --求每个班级的语文平均分 select banji,AVG(yuwen) from xs group by banji --求男女平均体重 select sex,avg(tizhong) as 平均体重 from xs group by sex --看看大于24岁的年龄 select nianling from xs where nianling>24 group by nianling --看看大于24岁的每个年龄的有多少个 select nianling,COUNT(*) from xs where nianling>24 group by nianling --看看大于24岁男女各多少个 select sex,COUNT(*)from xs where nianling>24 group by sex --看看20-25岁之间男女各多少个 select sex ,COUNT(*) from xs where nianling between 20 and 25 group by sex --查看每个班级英语成绩高于75的人数 select banji,COUNT(*) from xs where yingyu>75 group by banji --只查看语文成绩高于75并且人数多于5的班级 select banji,COUNT(*)from xs where yuwen>75 group by banji having COUNT(*)>5 --按照班级分组,查看班级人数,语文平均分,平均分在70以上的 select banji,avg(yuwen) from xs group by banji having avg(yuwen)>70 --按照班级分组,查看班级英语成绩超过75并且按照人数排列为降序 select banji,COUNT(*) from xs where yingyu>75 group by banji order by count(*) desc --group by分组语句进行顺序 --where、group by 、前面聚合函数、having或者其他order by
相关文章推荐
- 数据库创建两个表格及模糊查询,函数
- 关于MySQL数据库优化
- MySQL中怎么查询一张表的列数
- mysql 数据库的名称不能以数字开头
- MySQL 视图技术
- MySQL 视图技术
- SQL数据库基础知
- 数据库常见异常
- MySQL插入中文不乱码的5种方法
- 数据库操作中 pstmt.setString(1,id) ; 是什么意思 那个“1”指什么?
- 运维角度浅谈MySQL数据库优化
- mysql 表锁——读锁和写锁
- Oracle中执行存储过程call和exec区别
- nodejs、mongodb、Subversion环境搭建
- Mybatis 插入后返回数据库自动增长ID
- sqlserver中将某数据库下的所有表字段名称为小写的改为大写
- mysql定时备份
- SQL Server笔记2
- Mysql 技术内幕(二)
- left join中on和where的区别是什么