# 什么是 SQL 的 Aggregate Functions（聚合函数）

**URL:** <https://www.isharkfly.com/t/sql-aggregate-functions/15945>\
**Category:** 数据库\
**Tags:** mysql\
**Created:** [June 27, 2024, 6:36pm UTC](https://www.isharkfly.com/t/sql-aggregate-functions/15945 "2024-06-27T18:36:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [June 27, 2024, 6:36pm UTC](https://www.isharkfly.com/t/sql-aggregate-functions/15945/1 "2024-06-27T18:36:11Z")

</div>

它是⼀个返回单个值的数学函数。

SQL中的聚合函数是：

- AVG（）——返回平均值
- COUNT（）——返回⾏数
- MAX（）——返回最⼤值
- MIN（）——返回最⼩值
- ROUND（）——基于⼗进制规范，此函数对数字字段进⾏舍⼊
- SUM（）——返回总和

---

<div class="post-metadata">

**Author:** ![hex](https://www.isharkfly.com/user_avatar/www.isharkfly.com/hex/32/21953_2.png) [@hex](https://www.isharkfly.com/u/hex)\
**Post date:** [June 27, 2024, 6:36pm UTC](https://www.isharkfly.com/t/sql-aggregate-functions/15945/2 "2024-06-27T18:36:11Z")

</div>

MySQL 的聚合函数，请参考下官方的参考手册：[https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html)

| Name | Description |
| --- | --- |
| [`AVG()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_avg) | Return the average value of the argument |
| [`BIT_AND()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_bit-and) | Return bitwise AND |
| [`BIT_OR()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_bit-or) | Return bitwise OR |
| [`BIT_XOR()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_bit-xor) | Return bitwise XOR |
| [`COUNT()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_count) | Return a count of the number of rows returned |
| [`COUNT(DISTINCT)`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_count-distinct) | Return the count of a number of different values |
| [`GROUP_CONCAT()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_group-concat) | Return a concatenated string |
| [`JSON_ARRAYAGG()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_json-arrayagg) | Return result set as a single JSON array |
| [`JSON_OBJECTAGG()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_json-objectagg) | Return result set as a single JSON object |
| [`MAX()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_max) | Return the maximum value |
| [`MIN()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_min) | Return the minimum value |
| [`STD()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_std) | Return the population standard deviation |
| [`STDDEV()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_stddev) | Return the population standard deviation |
| [`STDDEV_POP()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_stddev-pop) | Return the population standard deviation |
| [`STDDEV_SAMP()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_stddev-samp) | Return the sample standard deviation |
| [`SUM()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_sum) | Return the sum |
| [`VAR_POP()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_var-pop) | Return the population standard variance |
| [`VAR_SAMP()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_var-samp) | Return the sample variance |
| [`VARIANCE()`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_variance) | Return the population standard variance |

 ![2025-04-18_10-41-42](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/5/f/5f8950bea1a57023d5ae067c5f44e1204035c5ec.jpeg)

不同版本的函数不一样，这个 8.4 的版本。

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [April 18, 2025, 2:39pm UTC](https://www.isharkfly.com/t/sql-aggregate-functions/15945/8 "2025-04-18T14:39:06Z")

</div>



---

<div class="post-metadata">

**Author:** ![hex](https://www.isharkfly.com/user_avatar/www.isharkfly.com/hex/32/21953_2.png) [@hex](https://www.isharkfly.com/u/hex)\
**Post date:** [April 18, 2025, 2:45pm UTC](https://www.isharkfly.com/t/sql-aggregate-functions/15945/9 "2025-04-18T14:45:27Z")

</div>

postgresql 的聚合函数功能多。

参考：[PostgreSQL: Documentation: 9.5: Aggregate Functions](https://www.postgresql.org/docs/9.5/functions-aggregate.html)

 ![2025-04-18_10-44-51](https://com-isharkfly-discourse.s3.dualstack.us-east-2.amazonaws.com/com-isharkfly-www/discourse-uploads/original/3X/5/1/511313565aa79fa15914c9e5cbbe2f031000077e.jpeg)

其实也就是一类函数的总称。

日常来说，count 天天用。
