SQL GROUP BY clause organizes a data set by grouping and aggregating data by columns. It is used to: Group tables by specified columns Apply aggregate functions (such as SUM, AVG, COUNT) to calculate group values
GROUP BY sub-sub in SQL Sentence
The GROUP BY clause is a powerful tool in SQL that allows users to group data sets by one or more columns and aggregate data for each group.
Function
The main functions of the GROUP BY clause are as follows:
Syntax
The syntax of the GROUP BY clause is as follows:
<code>SELECT 列名1, 列名2, ... FROM 表名 GROUP BY 列名3, 列名4, ...</code>
Usage example
The following is an example of using the GROUP BY clause:
<code>SELECT department, SUM(salary) FROM employees GROUP BY department</code>
This query Group the employees
table by the department
column and calculate the total salary for each department.
Aggregation Functions
The GROUP BY clause is often used with aggregate functions that calculate values for each group. Some common aggregate functions include:
Multiple column grouping
GROUP BY sub Sentences can be grouped by multiple columns at the same time. For example:
<code>SELECT department, location, SUM(salary) FROM employees GROUP BY department, location</code>
This query groups the employees
table by the department
and location
columns and calculates the total salary for each department and location.
The above is the detailed content of The role of groupby in sql. For more information, please follow other related articles on the PHP Chinese website!