The GROUP BY statement in SQL groups the data set by specified columns and performs aggregate calculations on each group. Usage is as follows: Identify grouping columns: Specify the columns to be grouped. Aggregation functions: Use SUM, COUNT, AVG and other functions to calculate grouped values. Grouped results: The query returns grouped results, showing the aggregated calculated value of each group.
GROUP BY Usage in SQL
The GROUP BY statement in SQL is used to group rows in a data set , and aggregate the data according to the grouping. It works by grouping rows that have the same value for a specific column.
Basic syntax:
<code class="sql">SELECT aggregate_function(column_name) FROM table_name GROUP BY column_name</code>
Usage:
Example:
Suppose we have a table containing student grades:
Student number | Name | Achievements |
---|---|---|
1 | John | 85 |
2 | Mary | 90 |
John | 95 | |
Susan | 80 |
<code class="sql">SELECT AVG(成绩) FROM students GROUP BY 姓名</code>
Average score | |
---|---|
90 | |
90 | |
80 |
The values in the grouping column must Have equal data types.
The above is the detailed content of Group by usage in sql. For more information, please follow other related articles on the PHP Chinese website!