Skip to content

Commit 33299c7

Browse files
MySQL Aggregate Functions.md
1 parent 6975125 commit 33299c7

File tree

1 file changed

+275
-0
lines changed

1 file changed

+275
-0
lines changed

MySQL Aggregate Functions.md

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
原文地址:https://www.mysqltutorial.org/mysql-aggregate-functions/
2+
3+
4+
5+
# MySQL Aggregate Functions
6+
7+
**Summary**: in this tutorial, you will learn about MySQL aggregate functions including `AVG`, `COUNT`, `SUM`, `MAX` and `MIN.`
8+
9+
## Introduction to MySQL aggregate functions
10+
11+
An aggregate function performs a calculation on multiple values and returns a single value.
12+
13+
For example, you can use the `AVG()` aggregate function that takes multiple numbers and returns the average value of the numbers.
14+
15+
The following illustrates the syntax of an aggregate function:
16+
17+
![image](https://github.com/user-attachments/assets/c66d31f3-ea73-44fc-84dc-7c472eeb7bee)
18+
19+
In this syntax:
20+
21+
- First, specify the name of the aggregate function e.g., `AVG()`. See the list of aggregate functions in the following section.
22+
- Second, use `DISTINCT` if you want to calculate based on distinct values or `ALL` in case you want to calculate all values including duplicates. The default is `ALL`.
23+
- Third, specify an expression that can be a column or an expression that involves column and arithmetic operators.
24+
25+
The aggregate functions are often used with the `GROUP BY` clause to calculate an aggregate value for each group e.g., the average value by the group or the sum of values in each group.
26+
27+
The following picture illustrates the `SUM()` aggregate function is used in conjunction with a `GROUP BY` clause:
28+
29+
![image](https://github.com/user-attachments/assets/5e967602-cb0d-451d-b113-ab6f6f48619e)
30+
31+
MySQL supports the following aggregate functions:
32+
33+
| Aggregate function | Description |
34+
| :----------------------------------------------------------- | :----------------------------------------------------------- |
35+
| [AVG()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-avg/) | Return the summation of all non-NULL values in a set. |
36+
| [BIT_AND()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-bit_and/) | Perform a bitwise AND of values in a column of a table. |
37+
| [BIT_OR()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-bit_or/) | Perform a bitwise OR of values in a column of a table. |
38+
| [BIT_XOR()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-bit_xor/) | Perform a bitwise XOR of values in a column of a table. |
39+
| [COUNT()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-count/) | Return the number of rows in a group, including rows with NULL values. |
40+
| [COUNT(DISTINCT)](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-count-distinct/) | Count the number of unique values of a column in a table. |
41+
| [COUNT(IF)](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-count-if/) | Count the number of values that meet a specified condition. |
42+
| [GROUP_CONCAT()](https://www.mysqltutorial.org/mysql-group_concat/) | Return a concatenated string. |
43+
| [JSON_ARRAYAGG()](https://www.mysqltutorial.org/mysql-json/mysql-json_arrayagg/) | Return result set as a single JSON array. |
44+
| [JSON_OBJECTAGG()](https://www.mysqltutorial.org/mysql-json/mysql-json_objectagg/) | Return result set as a single JSON object. |
45+
| [MAX()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-max-function/) | Return the highest value (maximum) in a set of non-NULL values. |
46+
| [MIN()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-min/) | Return the lowest value (minimum) in a set of non-NULL values. |
47+
| [STDEV()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-standard-deviation/) | Return the summation of all non-NULL values in a set. |
48+
| [STDDEV_POP()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-standard-deviation/) | Return the population standard deviation. |
49+
| [STDDEV_SAMP()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-standard-deviation/) | Return the sample standard deviation. |
50+
| [SUM()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-sum/) | Return the summation of all non-NULL values a set. |
51+
| [SUM(IF)](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-sum-if/) | Perform conditional summation using the SUM and IF functions. |
52+
| [VAR_POP()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-variance/) | Return the summation of all non-NULL values in a set. |
53+
| [VAR_SAMP()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-var_samp/) | Return the sample variance of values in a column of a table. |
54+
| [VARIANCE()](https://www.mysqltutorial.org/mysql-aggregate-functions/mysql-variance/) | Return the population standard variance of all non-NULL values in a set. |
55+
56+
57+
58+
## MySQL aggregate function examples
59+
60+
We will use the `products` and `orderdetails` tables from the [sample database](https://www.mysqltutorial.org/getting-started-with-mysql/mysql-sample-database/) for demonstration:
61+
62+
![image](https://github.com/user-attachments/assets/f4e2c839-0315-42f1-9c41-db6fc05e582b)
63+
64+
### The AVG() function examples
65+
66+
The `AVG()` function calculates the average value of a set of values. It ignores NULL in the calculation.
67+
68+
```
69+
AVG(expression)Code language: SQL (Structured Query Language) (sql)
70+
```
71+
72+
For example, you can use the `AVG` function to calculate the average buy price of all products in the `products` table by using the following query:
73+
74+
```
75+
SELECT
76+
AVG(buyPrice) average_buy_price
77+
FROM
78+
products;Code language: SQL (Structured Query Language) (sql)
79+
```
80+
81+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#1)
82+
83+
![MySQL Aggregate Function - AVG example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-AVG-example.png)
84+
85+
The following example uses the `AVG()` function to calculate the average buy price for each product line:
86+
87+
```
88+
SELECT
89+
productLine,
90+
AVG(buyPrice)
91+
FROM
92+
products
93+
GROUP BY productLine
94+
ORDER BY productLine;
95+
Code language: SQL (Structured Query Language) (sql)
96+
```
97+
98+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#1)
99+
100+
![MySQL Aggregate Function - AVG with GROUP BY example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-AVG-with-GROUP-BY-example.png)
101+
102+
### The COUNT() function examples
103+
104+
The `COUNT()` function returns the number of the values in a set.
105+
106+
For example, you can use the `COUNT()` function to get the number of products in the `products` table as shown in the following query:
107+
108+
```
109+
SELECT
110+
COUNT(*) AS total
111+
FROM
112+
products;Code language: PHP (php)
113+
```
114+
115+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#4)
116+
117+
![MySQL Aggregate Function - COUNT example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-COUNT-example.png)
118+
119+
The following statement uses the `COUNT()` function with the `GROUP BY` clause to get the number of products for each product line:
120+
121+
```
122+
SELECT
123+
productLine,
124+
COUNT(*)
125+
FROM
126+
products
127+
GROUP BY productLine
128+
ORDER BY productLine;
129+
Code language: SQL (Structured Query Language) (sql)
130+
```
131+
132+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#1)
133+
134+
![MySQL Aggregate Function - COUNT with GROUP BY example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-COUNT-with-GROUP-BY-example.png)
135+
136+
### The SUM() function examples
137+
138+
The `SUM()` function returns the sum of values in a set. The `SUM()` function ignores `NULL`. If no matching row is found, the `SUM()` function returns NULL.
139+
140+
To get the total order value of each product, you can use the `SUM()` function in conjunction with the `GROUP BY` clause as follows:
141+
142+
```
143+
SELECT
144+
productCode,
145+
SUM(priceEach * quantityOrdered) total
146+
FROM
147+
orderDetails
148+
GROUP BY productCode
149+
ORDER BY total DESC;Code language: SQL (Structured Query Language) (sql)
150+
```
151+
152+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#6)
153+
154+
![MySQL Aggregate Function - SUM example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-SUM-example.png)
155+
156+
To see the result in more detail, you can [join](https://www.mysqltutorial.org/mysql-basics/mysql-join/) the `orderdetails` table to the `products` table as shown in the following query:
157+
158+
```
159+
SELECT
160+
productCode,
161+
productName,
162+
SUM(priceEach * quantityOrdered) total
163+
FROM
164+
orderDetails
165+
INNER JOIN
166+
products USING (productCode)
167+
GROUP BY productCode
168+
ORDER BY total;Code language: SQL (Structured Query Language) (sql)
169+
```
170+
171+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#1)
172+
173+
![MySQL Aggregate Function - SUM with JOIN example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-SUM-with-JOIN-example.png)
174+
175+
### The MAX() function examples
176+
177+
The `MAX()` function returns the maximum value in a set.
178+
179+
```
180+
MAX(expression)Code language: SQL (Structured Query Language) (sql)
181+
```
182+
183+
For example, you can use the `MAX()` function to get the highest buy price from the `products` table as shown in the following query:
184+
185+
```
186+
SELECT
187+
MAX(buyPrice) highest_price
188+
FROM
189+
products;Code language: SQL (Structured Query Language) (sql)
190+
```
191+
192+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#8)
193+
194+
![MySQL Aggregate Function - MAX example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-MAX-example.png)
195+
196+
The following statement uses the `MAX()` function with the `GROUP BY` clause to get the highest price per product line:
197+
198+
```
199+
SELECT
200+
productLine, MAX(buyPrice)
201+
FROM
202+
products
203+
GROUP BY productLine
204+
ORDER BY MAX(buyPrice) DESC;Code language: SQL (Structured Query Language) (sql)
205+
```
206+
207+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#1)
208+
209+
![MySQL Aggregate Function - MAX with GROUP BY example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-MAX-with-GROUP-BY-example.png)
210+
211+
### The MIN() function examples
212+
213+
The `MIN()` function returns the minimum value in a set of values.
214+
215+
```
216+
MIN(expression)Code language: SQL (Structured Query Language) (sql)
217+
```
218+
219+
For example, the following query uses the `MIN()` function to find the lowest price from the `products` table:
220+
221+
```
222+
SELECT
223+
MIN(buyPrice) lowest_price
224+
FROM
225+
products;Code language: SQL (Structured Query Language) (sql)
226+
```
227+
228+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#11)
229+
230+
![MySQL Aggregate Function - MIN example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-MIN-example.png)
231+
232+
The following example uses the `MIN()` function with the `GROUP BY` clause to get the lowest price per product line:
233+
234+
```
235+
SELECT
236+
productLine,
237+
MIN(buyPrice)
238+
FROM
239+
products
240+
GROUP BY productLine
241+
ORDER BY MIN(buyPrice);Code language: SQL (Structured Query Language) (sql)
242+
```
243+
244+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#1)
245+
246+
![MySQL Aggregate Function - MIN with GROUP BY example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-MIN-with-GROUP-BY-example.png)
247+
248+
### The GROUP_CONCAT() function example
249+
250+
The `GROUP_CONCAT()` concatenates a set of strings and returns the concatenated string. See the following `employees` and `customers` tables:
251+
252+
![img](https://www.mysqltutorial.org/wp-content/uploads/2019/09/customers-employees.png)
253+
254+
The following statement uses the `GROUP_CONCAT()` function to return the sales staff and list of customers that each sales staff is in charge of:
255+
256+
```
257+
SELECT
258+
firstName,
259+
lastName,
260+
GROUP_CONCAT(
261+
DISTINCT customername
262+
ORDER BY customerName) customers
263+
FROM
264+
employees
265+
INNER JOIN customers
266+
ON customers.salesRepEmployeeNumber = employeeNumber
267+
GROUP BY employeeNumber
268+
ORDER BY firstName , lastname;Code language: SQL (Structured Query Language) (sql)
269+
```
270+
271+
[Try It Out](https://www.mysqltutorial.org/tryit/query/mysql-aggregate-functions/#1)
272+
273+
![MySQL Aggregate Function - GROUP_CONCAT example](https://www.mysqltutorial.org/wp-content/uploads/2019/09/MySQL-Aggregate-Function-GROUP_CONCAT-example.png)
274+
275+
In this tutorial, you have learned how to use the most commonly used MySQL aggregate functions.

0 commit comments

Comments
 (0)