What is the PARTITION BY Clause?
The PARTITION BY clause is a type of clause that is used with window functions to divide the result set into partitions on which the function is applied. It allows us to perform calculations and aggregations within each partition independently.
Syntax:
SELECT column1, column2, ..., function(column) OVER (PARTITION BY column_name1, column_name2, ...)
FROM table_name;
Explanation: Here the function(column)
is the window function that we want to apply and column_name1, column_name
2
are the columns which are used to partition the result set.
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.