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

Three ways to check whether a port is open in a remote Linux system

This is a very important topic, not only for Linu...

js to achieve a simple lottery function

This article shares the specific code of js to im...

HTML weight loss Streamline HTML tags to create web pages

HTML 4 HTML (not XHTML), MIME type is text/html, ...

SQL implementation of LeetCode (178. Score ranking)

[LeetCode] 178.Rank Scores Write a SQL query to r...

Learn more about MySQL indexes

1. Indexing principle Indexes are used to quickly...

5 issues you should pay attention to when making a web page

1. Color matching problem <br />A web page s...

React handwriting tab switching problem

Parent File import React, { useState } from '...

MySQL 5.7 cluster configuration steps

Table of contents 1. Modify the my.cnf file of se...

Mini Program to Implement Sieve Lottery

This article example shares the specific code of ...

Sample code for CSS dynamic loading bar effect

Using the knowledge of CSS variables, I will dire...

Implementation of Vue single file component

I recently read about vue. I found a single-file ...

Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)

Wildcard categories: %Percent wildcard: indicates...

The pitfall of MySQL numeric type auto-increment

When designing table structures, numeric types ar...