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 is a very important topic, not only for Linu...
This article shares the specific code of js to im...
HTML 4 HTML (not XHTML), MIME type is text/html, ...
[LeetCode] 178.Rank Scores Write a SQL query to r...
1. Indexing principle Indexes are used to quickly...
1. Color matching problem <br />A web page s...
Parent File import React, { useState } from '...
Table of contents 1. Modify the my.cnf file of se...
This article example shares the specific code of ...
Using the knowledge of CSS variables, I will dire...
I recently read about vue. I found a single-file ...
Copy code The code is as follows: <HTML> &l...
Wildcard categories: %Percent wildcard: indicates...
Preface: This article only introduces the steps t...
When designing table structures, numeric types ar...