GROUP BY in SQL is used to group data sets and perform summary operations. The grouping column specifies the column of the grouped data set, while the summary operation specifies the operation to perform (such as a sum or count). Example: SELECT product_category, SUM(quantity_sold) FROM sales_table GROUP BY product_category; Group sales by product category and calculate total sales for each category.
Usage of GROUP BY in SQL
The GROUP BY clause is used to group the data set so that each Groups perform summary operations such as sums, averages, or counts.
Syntax:
<code class="sql">SELECT column_list FROM table_name WHERE condition GROUP BY group_by_column_list</code>
Usage:
Perform summary operation (column_list): This list specifies the summary operation to be performed on each group, for example:
Example:
Calculate total sales for each product category:
<code class="sql">SELECT product_category, SUM(quantity_sold) FROM sales_table GROUP BY product_category;</code>
The output will group each product category and show the total sales for each category.
Other notes:
GROUP BY product_category, product_name
. GROUP BY 1
, where 1
represents the first column of the data set. MIN()
and MAX()
. The above is the detailed content of Usage of group by in sql. For more information, please follow other related articles on the PHP Chinese website!