In SQL, the GROUP BY clause is used to group and calculate aggregate values based on specified columns: Group data: Organize data into groups based on specific columns. Calculate aggregate values: Perform summary calculations, such as sums, averages, or counts, on column values for each group.
Meaning of GROUP BY
In SQL, GROUP BY
clause is used Groups data based on specified columns and calculates aggregate values for each group. Aggregated values are obtained by applying a specific operation (such as sum, average, or count) to the values of all rows in the group.
How to use GROUP BY
The GROUP BY
clause is used in the SELECT
statement, and its syntax format is as follows:
<code class="sql">SELECT 聚合函数(列名) FROM 表名 GROUP BY 列名1, 列名2, ...</code>
Among them:
Aggregation function
is the operation to be performed on the values within the group, such as SUM()
, AVG ()
, or COUNT()
. Column name
is the column used to group the data. Example
Consider the following table:
Name | Category | Achievements |
---|---|---|
张三 | MATHEMATICS | 80 |
李四 | MATHEMATICS | 90 |
English | 75 | |
English | 85 |
<code class="sql">SELECT 类别, AVG(成绩) FROM 表名 GROUP BY 类别;</code>
the result will be:
Average Grade | |
---|---|
English | |
The above is the detailed content of The meaning of group by in sql. For more information, please follow other related articles on the PHP Chinese website!