当前位置:网站首页>Use of aggregate functions
Use of aggregate functions
2022-07-03 19:35:00 【Piglet get】
Catalog
One 、 Aggregate function introduction
Ⅰ. How to realize the number of records in the calculation table ?
Ⅲ. Query the average bonus rate in the company
Use count(*),count(1),count( Specific fields ) Which is more efficient ?
demand 1: Query the average salary of each department , Maximum wage
demand 2: Check each one department_id,job_id The average wage of
2. practice : Query the highest salary ratio in each department 10000 High department information
Four 、SQL The underlying execution principle
( One )WHERE And HAVING Comparison of
2. If the filter condition has no aggregate function
( 3、 ... and )SQL Statement execution
One 、 Aggregate function introduction
1. Concept
Aggregate functions act on a set of data , And return a value for a set of data .
2. Type of aggregate function
①AVG and SUM function
Only applicable to fields of numeric type ( Or variable )
SELECT AVG(salary),SUM(salary),AVG(salary)*107
FROM employees;
# The following operations are meaningless
SELECT SUM(last_name),AVG(last_name),SUM(hire_date)
FROM employees;②MIN and MAX function
For numeric types 、 String type 、 Fields of date time type ( Or variable )
SELECT MAX(salary),MIN(salary),MAX(hire_date),MIN(hire_date)
FROM employees;
③COUNT function
Calculate the number of specified fields in the query structure
SELECT COUNT(employee_id),COUNT(salary),COUNT(2*salary),COUNT(1)
FROM employees;Ⅰ. How to realize the number of records in the calculation table ?
The way 1:COUNT(*)
The way 2:COUNT(1)
The way 3:COUNT( Specific fields ) —— Not necessarily right
Be careful : When calculating the number of occurrences of the specified field , It is not calculated NULL It's worth it
Ⅱ.AVG = SUM/COUNT
SELECT AVG(salary),SUM(salary)/COUNT(salary),
AVG(commission_pct),SUM(commission_pct)/COUNT(commission_pct),
SUM(commission_pct) / 107
FROM employees;

Ⅲ. Query the average bonus rate in the company
# Wrong writing
SELECT AVG(commission_pct)
FROM employees;
# Write it correctly
SELECT SUM(commission_pct) / COUNT(IFNULL(commission_pct,0)),
AVG(IFNULL(commission_pct,0))
FROM employees;Use count(*),count(1),count( Specific fields ) Which is more efficient ?
If you are using MYISAM Storage engine , Then the efficiency of the three is the same , All are O(1)
If you are using InnoDB Storage engine , Then the efficiency of the three count(*) = count(1) > count( Specific fields )
Two 、GROUP BY Use
demand 1: Query the average salary of each department , Maximum wage
SELECT AVG(salary)
FROM employees; # The average salary of the whole company
SELECT department_id,AVG(salary),MAX(salary)
FROM employees
GROUP BY department_id;
demand 2: Check each one department_id,job_id The average wage of
SELECT department_id,job_id,AVG(salary)
FROM employees
GROUP BY department_id,job_id;
Conclusion
1.SELECT Fields of non group functions appearing in , It must be stated that in GROUP BY in . conversely ,GROUP BY Fields declared in can not appear in SELECT in
2.GROUP BY The statement in FROM Back 、WHERE Back ,ORDER BY front 、LIMIT front
3. stay MySQL Use in WITH ROLLUP: Add a record after all the queried grouping records , This record Calculate the sum of all the records queried , That is, count the number of records .
3、 ... and 、HAVING Use
1. effect
Filtering data
2. practice : Query the highest salary ratio in each department 10000 High department information
# Wrong writing
SELECT department_id,MAX(salary)
FROM employees
WHERE MAX(salary) > 10000
GROUP BY department_id;
# Write it correctly
SELECT department_id,MAX(salary)
FROM employees
GROUP BY department_id
HAVING MAX(salary) > 10000;
requirement
- If an aggregate function is used in the filter condition , Must be used HAVING To replace WHERE, Otherwise, an error will be reported
- HAVING It must be stated that in GROUP BY Behind
- In development , Use HAVING The premise is SQL Used in GROUP BY
Four 、SQL The underlying execution principle
When there is an aggregate function in the filter condition , Then this filter condition must be declared in HAVING in
When there is no aggregate function in the filter condition , Then this filter condition is declared in WHERE Medium or HAVING Either way , however , It is suggested that you declare in WHERE in
( One )WHERE And HAVING Comparison of
1. Scope of application
HAVING Has a wider scope of application
2. If the filter condition has no aggregate function
WHERE More efficient than HAVING

summary

( Two )SELECT sentence
1.SQL92 The grammar of
SELECT ......( There are aggregate functions )
FROM ......
WHERE Connection conditions of multiple tables AND Filter conditions that do not contain aggregate functions
GROUP BY ......
HAVING Filter conditions containing aggregate functions
ORDER BY ......(ASC / DESC)
LIMIT ......
2.SQL99 The grammar of
SELECT ......( There are aggregate functions )
FROM .... (LEFT / RIGHT) JOIN ... ON Connection conditions of multiple tables
(LEFT / RIGHT) JOIN ... ON ...
WHERE Filter conditions that do not contain aggregate functions
GROUP BY ......
HAVING Filter conditions containing aggregate functions
ORDER BY ......(ASC / DESC)
LIMIT ......
( 3、 ... and )SQL Statement execution

FROM ...... --> ON --> (LEFT / RIGHT JOIN) --> WHERE --> GROUP BY --> HAVING --> SELECT --> DISTINCT( duplicate removal ) --> ORDER BY( Sort ) --> LIMIT( Pagination display )
边栏推荐
- Day11 ---- 我的页面, 用户信息获取修改与频道接口
- Ego planner code parsing Bspline_ Optimizer section (2)
- Flume learning notes
- 第一章:递归求n的阶乘n!
- Luogu-p1107 [bjwc2008] Lei Tao's kitten
- Cross compile opencv with contrib
- Thesis study - 7 Very Deep Convolutional Networks for Large-Scale Image Recognition (3/3)
- Chapter 1: extend the same code decimal sum s (D, n)
- FBI warning: some people use AI to disguise themselves as others for remote interview
- Summary of learning materials and notes of Zhang Fei's actual combat electronics 1-31
猜你喜欢

Basic principle of LSM tree

Detailed explanation of shuttle unity interworking principle

第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组

第一章:求所有阶乘和数,大奖赛现场统分程序设计,三位阶乘和数,图形点扫描,递归求n的阶乘n!,求n的阶乘n!,舍罕王失算

Chapter 1: extend the same code decimal sum s (D, n)

Free year-end report summary template Welfare Collection

Yolov3 network model building

PR 2021 quick start tutorial, material import and management

kubernetes集群搭建efk日志收集平台
![[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD](/img/51/209e35e0b94a51b3b406a184459475.png)
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
随机推荐
Free year-end report summary template Welfare Collection
2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
Zhang Fei hardware 90 day learning notes - personal record of day 3, please see my personal profile / homepage for the complete
Sentinel source code analysis part I sentinel overview
If the warehouse management communication is not in place, what problems will occur?
第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[x,y]内的守形数,探求n位守形数,递推探索n位逐位整除数
Day_ 18 IO stream system
The space of C disk is insufficient, and the computer becomes stuck. Quickly expand the capacity of C disk to make the system more smooth
第一章:拓广同码小数和s(d, n)
Ego planner code parsing Bspline_ Optimizer section (3)
PR 2021 quick start tutorial, how to create a new sequence and set parameters?
Day11 ---- 我的页面, 用户信息获取修改与频道接口
What is the content of game modeling
CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析
Using the visualization results, click to appear the corresponding sentence
第二章:求长方体数组,指定区间内的完全数,改进指定区间内的完全数
Chapitre 1: le roi de shehan a mal calculé
Pecan — @expose()
BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply