SQL GROUP BY
The GROUP BY statement groups rows that have the same values into summary rows. It is commonly used with aggregate functions like COUNT
, SUM
, AVG
, etc., to group and summarize data.
Syntax
The basic syntax for GROUP BY is:
SELECT column1, aggregate_function(column2)
FROM table
GROUP BY column1;
Example
Suppose we have a table Sales
with columns Product
and Amount
. To find the total sales amount for each product:
SELECT Product, SUM(Amount) as TotalSales
FROM Sales
GROUP BY Product;