当前位置:网站首页>多行函数;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;
边栏推荐
- 命令模式(Command)
- What is the terminal privilege management
- *W3C* Standards Organization
- 临床研究方法学,到现场,到数据真实发生的地方 | 对话数智 x 张维拓
- 小程序实战(一)- 骨架屏的应用与实现
- 喂,你知道节流是什么吗?
- Leetcode - using sequence traversal features first completed 114. The binary tree to the list
- 【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
- 从零开始Blazor Server(7)--使用Furion权限验证
- 使用.NET简单实现一个Redis的高性能克隆版(二)
猜你喜欢

【LeetCode】653. 两数之和 IV - 输入 BST

The sword refers to the Great Wall Cannon?Official spy photos of Changan's new pickup

Oracle中对临时表空间执行shrink操作

ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法

Graphical Hands-on Tutorial--ESP32 One-Key Network Configuration (Smartconfig, Airkiss)

audio_policy_configuration.xml配置文件详解

Learn to use the basic interface of set and map

【励志】复盘的重要性

【LeetCode】98.验证二叉搜索树

什么是 DevOps?看这一篇就够了!
随机推荐
What is the principle of thermal imaging temperature measurement?Do you know?
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
3-5年以上的功能测试如何进阶自动化?
Redis查询缓存
数字知识库及考学一体化平台
音频编辑 合唱
知乎数据分析训练营
ping的原理
【机器学习】:如何对你的数据进行分类?
Maple 2022 software installation package download and installation tutorial
北京大学,新迎3位副校长!其中一人为中科院院士!
*iframe*
遍历Map的四种方法
【LeetCode】899.有序队列
小程序实战(三) - head组件的封装与使用
cubemx stm32 afm3000 module gas flow sensor driver code
Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
Xilinx VIVADO 中 DDR3(Naive)的使用(2)读写设计
123
Learn to use the basic interface of set and map