What is GROUP BY Clause?
The GROUP BY clause is used to aggregate data based on one or more columns and group rows with identical values into summary rows. It is widely used via aggregate functions like SUM, COUNT, and AVG to perform calculations on each group.
Syntax:
SELECT column1, column2, aggregate_function(column3)
FROM table_name
WHERE condition
GROUP BY column1, column2;
Explanation:
column1
,column2
are the columns that we want to group by.aggregate_function
is the function likeSUM
,COUNT
,AVG
,MAX
,MIN
that we want to apply to the grouped data incolumn3
.table_name
is the name of the table.condition
is an optional condition to filter the rows before grouping.
PARTITION BY vs GROUP BY in SQL
In SQL both PARTITION BY and GROUP BY are important clauses used for data aggregation and analysis. Sometimes they work as same but they serve different purposes and are applied in different situations. In this article, we’ll understand both of them along with the syntax, multiple examples for both clauses and also the differences between them.