1. Simple to use SUM: sum (generally used to process numerical 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; 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; 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: Therefore, 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; #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; #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; #SELECT COUNT(*) AS number of employees, job_id, department_id by multiple fields FROM employees GROUP BY job_id,department_id; #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; 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:
|
<<: Introduction to deploying selenium crawler program under Linux system
>>: Use HTML and CSS to create your own warm man "Dabai"
This article describes how to install opencv with...
This article introduces the sample code for imple...
mysql returns Boolean type In the first case, ret...
Table of contents Preface Related Materials Achie...
Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...
one. First of all, you have to package it in idea...
Preface The reason for writing this article is mai...
In the latest HTML standard, there is a calc CSS e...
Preface MySQL 8.0.13 began to support index skip ...
I don't expect to be an expert DBA, but when ...
1. Basic syntax structure of HTML submit and bott...
Written in front When we operate the database in ...
Overview What is harbor? The English word means: ...
Preface: The group by function retrieves the firs...
Table of contents Vue.js 1. Register global guard...