MySQL essential basics: grouping function, aggregate function, grouping query detailed explanation

MySQL essential basics: grouping function, aggregate function, grouping query detailed explanation

1. Simple to use

SUM: sum (generally used to process numerical values)
AVG: Average (generally used to process numerical values)
MAX: Maximum (can also be used to process strings and dates)
MIN: minimum (can also be used to process strings and dates)
COUNT: Quantity (count the number of non-null values)

The above grouping functions ignore NULL values.

SELECT SUM(salary) AS sum, AVG(salary) AS average, MAX(salary) AS maximum, MIN(salary) AS minimum, COUNT(salary) AS number FROM employees;

insert image description here

2. Use DISTINCT to remove duplicates

(All of the above functions are available)

SELECT SUM(DISTINCT salary) AS sum, AVG(DISTINCT salary) AS average, COUNT(DISTINCT salary) AS number of duplicates removed, COUNT(salary) AS number of non-duplicates FROM employees;

insert image description here

3. Detailed introduction of COUNT()

#Equivalent to the method of counting rows SELECT COUNT(*) 
FROM employees;
#Equivalent to the second method of counting rows, where 1 can be replaced by other constants or fields SELECT COUNT(1) 
FROM employees;

Efficiency issues:
COUNT(*) is highly efficient under the MYISAM storage engine
In the INNODB storage engine, COUNT(*) and COUNT(1) are almost as efficient, but higher than COUNT(字段)

Therefore, COUNT(*) is generally used to count the number of rows.

4. Group Query

#[] contains optional SELECT grouping functions and lists (required to appear after GROUP BY)
FROM table [WHERE filter condition]
GROUP BY grouping list [ORDER BY clause]

Example:

#Query the highest salary for each job type SELECT MAX(salary) AS highest salary, job_id
FROM employees
GROUP BY job_id;

insert image description here

#Query the average salary of employees whose emails contain a in each department (screening before grouping)
SELECT AVG(salary) AS average salary, department_id
FROM employees
WHERE email LIKE '%a%'
GROUP BY department_id;

insert image description here

#Query the number of employees in departments where the number of employees is greater than 2 (screening after grouping)
#Using HAVING
SELECT COUNT(*) AS employee_number,department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*)>2;

insert image description here

insert image description here

#SELECT COUNT(*) AS number of employees, job_id, department_id by multiple fields
FROM employees
GROUP BY job_id,department_id;

insert image description here

#Complete structure SELECT AVG(salary) AS average salary, department_id
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id
HAVING AVG(salary)>9000
ORDER BY AVG(salary) DESC;

insert image description here

This concludes this article on MySQL's essential basics of grouping functions, aggregate functions, and grouped queries. For more information about MySQL grouping functions, please search 123WORDPRESS.COM's previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Very practical MySQL function comprehensive summary detailed example analysis tutorial
  • MySQL practical window function SQL analysis class students' test scores and living expenses
  • MySQL Database Basics SQL Window Function Example Analysis Tutorial
  • mysql calculation function details
  • MySQL example to explain single-row functions and character math date process control
  • A brief introduction to MySQL functions
  • MySQL spatial data storage and functions
  • Comprehensive summary of mysql functions

<<:  Introduction to deploying selenium crawler program under Linux system

>>:  Use HTML and CSS to create your own warm man "Dabai"

Recommend

Detailed explanation of the correct way to install opencv on ubuntu

This article describes how to install opencv with...

A brief discussion on several situations where MySQL returns Boolean types

mysql returns Boolean type In the first case, ret...

Vue+Openlayer realizes the dragging and rotation deformation effect of graphics

Table of contents Preface Related Materials Achie...

Solution to the problem that Centos8 cannot install docker

Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...

Analysis of the project process in idea packaging and uploading to cloud service

one. First of all, you have to package it in idea...

HTML left, center, right adaptive layout (using calc css expression)

In the latest HTML standard, there is a calc CSS e...

Index Skip Scan in MySQL 8.0

Preface MySQL 8.0.13 began to support index skip ...

Optimize MySQL with 3 simple tweaks

I don't expect to be an expert DBA, but when ...

Optimizing the slow query of MySQL aggregate statistics data

Written in front When we operate the database in ...

Detailed tutorial on installing harbor private warehouse using docker compose

Overview What is harbor? The English word means: ...

Implement group by based on MySQL to get the latest data of each group

Preface: The group by function retrieves the firs...