当前位置:网站首页>多行函数;group_by分组;having分组后筛选;单表查询总结
多行函数;group_by分组;having分组后筛选;单表查询总结
2022-08-04 11:16:00 【parker_001】
对一组数据进行运算,针对一组数据(多行记录)只返回一个结果,也称分组函数

多行函数包含:

-- 多行函数:
select max(sal),min(sal),count(sal),sum(sal),sum(sal)/count(sal),avg(sal) from emp;
select * from emp;
-- 多行函数自动忽略null值
select max(comm),min(comm),count(comm),sum(comm),sum(comm)/count(comm),avg(comm) from emp;
-- max(),min(),count()针对所有类型 sum(),avg() 只针对数值型类型有效
select max(ename),min(ename),count(ename),sum(ename),avg(ename) from emp;
-- count --计数
-- 统计表的记录数:方式1:
select * from emp;
select count(ename) from emp;
select count(*) from emp;
-- 统计表的记录数:方式2
select 1 from dual;
select 1 from emp;
select count(1) from emp;
group_by分组
【1】group by : 用来进行分组
【2】sql展示:
select * from emp;
-- 统计各个部门的平均工资
select deptno,avg(sal) from emp; -- 字段和多行函数不可以同时使用
select deptno,avg(sal) from emp group by deptno; -- 字段和多行函数不可以同时使用,除非这个字段属于分组
select deptno,avg(sal) from emp group by deptno order by deptno desc;
-- 统计各个岗位的平均工资
select job,avg(sal) from emp group by job;
select job,lower(job),avg(sal) from emp group by job;
having分组后筛选
-- 统计各个部门的平均工资 ,只显示平均工资2000以上的 - 分组以后进行二次筛选 having
select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000;
select deptno,avg(sal) 平均工资 from emp group by deptno having 平均工资 > 2000;
select deptno,avg(sal) 平均工资 from emp group by deptno having 平均工资 > 2000 order by deptno desc;
-- 统计各个岗位的平均工资,除了MANAGER
-- 方法1:
select job,avg(sal) from emp where job != 'MANAGER' group by job;
-- 方法2:
select job,avg(sal) from emp group by job having job != 'MANAGER' ;
-- where在分组前进行过滤的,having在分组后进行后滤。
单表查询总结
【1】select语句总结
select column, group_function(column)
from table
[where condition]
[group by group_by_expression]
[having group_condition]
[order by column];
注意:顺序固定,不可以改变顺序
【2】select语句的执行顺序
from--where -- group by– select - having- order by
【3】单表查询练习:
-- 单表查询练习:
-- 列出工资最小值小于2000的职位
select job,min(sal)
from emp
group by job
having min(sal) < 2000 ;
-- 列出平均工资大于1200元的部门和工作搭配组合
select deptno,job,avg(sal)
from emp
group by deptno,job
having avg(sal) > 1200
order by deptno;
-- 统计[人数小于4的]部门的平均工资。
select deptno,count(1),avg(sal)
from emp
group by deptno
having count(1) < 4
-- 统计各部门的最高工资,排除最高工资小于3000的部门。
select deptno,max(sal)
from emp
group by deptno
having max(sal) < 3000;
边栏推荐
- 北京大学,新迎3位副校长!其中一人为中科院院士!
- cubemx stm32 afm3000模块 气体流量传感器 驱动代码
- Meishe Q&A Room | Meiying VS Meishe Cloud Editing
- ORB-SLAM3中的优化
- Jenkins User Manual (1) - Software Installation
- 剑指长城炮? 长安全新皮卡官方谍照
- Small program containers accelerate the construction of an integrated online government service platform
- MATLAB程序设计与应用 3.2 矩阵变换
- 美摄问答室|美映 VS 美摄云剪辑
- 小程序实战(三) - head组件的封装与使用
猜你喜欢

命令模式(Command)

热成像测温的原理是什么呢?你知道吗?

Small program containers accelerate the construction of an integrated online government service platform

Redis查询缓存

Leetcode刷题——二叉搜索树相关题目(98. 验证二叉搜索树、235. 二叉搜索树的最近公共祖先、1038. 从二叉搜索树到更大和树、538. 把二叉搜索树转换为累加树)

Mysql高级篇学习总结13:多表连接查询语句优化方法(带join语句)

Learn to use the basic interface of set and map

深度强化学习与APS的一些感想

Meishe Q&A Room | Meiying VS Meishe Cloud Editing

123
随机推荐
Leetcode——利用先序遍历特性完成114. 二叉树展开为链表
【虹科案例】基于3D相机组装家具
使用json-server快速搭建本地数据接口
mysqldump远程备份数据库
Leetcode刷题——路径总和
第二批养老理财试点产品发行 一小时销售20亿元
Super Learning Method
你值得拥有的登录注册页面(附赠源码)
Leetcode brush questions - binary search tree related topics (98. Verify binary search tree, 235. The nearest common ancestor of binary search tree, 1038. From binary search tree to bigger sum tree, 5
【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
iMeta | German National Cancer Center Gu Zuguang published a complex heatmap visualization method
DB2查看执行过长的SQL
Heap Sort
JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
学会使用set和map的基本接口
【LeetCode】232.用栈实现队列
shell变量
MTBF是什么意思?交换机做MTBF有什么要求?MTTF、MTBF和MTTR的区别是什么?
萌宠来袭,如何让“吸猫撸狗”更有保障?
什么是终端特权管理