当前位置:网站首页>多行函数;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;
边栏推荐
猜你喜欢
CVPR 2022 | 从人体网格预测骨架,是真正的生理学骨架!
What is the principle of thermal imaging temperature measurement?Do you know?
Small program containers accelerate the construction of an integrated online government service platform
命令模式(Command)
美摄问答室|美映 VS 美摄云剪辑
Leetcode刷题——二叉搜索树相关题目(98. 验证二叉搜索树、235. 二叉搜索树的最近公共祖先、1038. 从二叉搜索树到更大和树、538. 把二叉搜索树转换为累加树)
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
The use of DDR3 (Naive) in Xilinx VIVADO (3) simulation test
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
强烈推荐一款优秀且通用的后台管理系统
随机推荐
【Inspirational】The importance of review
cubemx stm32 afm3000 module gas flow sensor driver code
Win11 file types, how to change?Win11 modify the file suffix
cat /proc/kallsyms 发现内核符号表值都为0
Jenkins User Manual (1) - Software Installation
MATLAB程序设计与应用 3.2 矩阵变换
3-5年以上的功能测试如何进阶自动化?
Mysql高级篇学习总结13:多表连接查询语句优化方法(带join语句)
Four ways to traverse a Map
apache dolphin scheduler 文件dolphinscheduler-daemon.sh详解
中介者模式(Mediator)
*iframe*
秒云成功入选《2022爱分析 · 银行数字化厂商全景报告》,智能运维能力获认可
audio_policy_configuration.xml配置文件详解
什么是终端特权管理
Graphical Hands-on Tutorial--ESP32 One-Key Network Configuration (Smartconfig, Airkiss)
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
Using .NET to simply implement a high-performance clone of Redis (2)
Leetcode——利用先序遍历特性完成114. 二叉树展开为链表
图文手把手教程--ESP32 OTA空中升级(VSCODE+IDF)