group by sql
时间: 2023-08-25 07:05:27 浏览: 69
GROUP BY is a clause used in SQL to group rows based on one or more columns. It is often used in combination with aggregate functions like SUM, COUNT, AVG, etc. The basic syntax of a GROUP BY statement is as follows:
```
SELECT column1, aggregate_function(column2)
FROM table
GROUP BY column1;
```
In this syntax, `column1` is the column you want to group by, and `aggregate_function` is the function you want to apply to `column2`. The result set will be grouped by `column1`, and the aggregate function will be applied to each group.
For example, let's say we have a table called "employees" with columns like "department" and "salary". If we want to calculate the average salary for each department, we can use the following query:
```
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
```
This query will group the rows by the "department" column and calculate the average salary for each department.
阅读全文