How to Use Group By and Partition By in SQL | by Chi Nguyen | Towards Data Science

When I first learned SQL, I had a problem of differentiating between PARTITION BY and GROUP BY, as they both have a function for grouping. I believe many people who begin to work with SQL may encounter the same problem. Therefore, in this article I want to share with you some examples of using PARTITION BY, and the difference between it and GROUP BY in a select statement.
Firstly, I create a simple dataset with 4 columns. The df table below describes the amount of money and type of fruit that each employee in different functions will bring in their company trip.
The following is the syntax of Partition By:
SELECT expression 1, expression 2, ...aggregate function () OVER (PARTITION BY expression 1 order_clause frame_clause)FROM table
When we want to do an aggregation on a specific column, we can apply PARTITION BY clause with the OVER clause. Let’s look at the example below to see how the dataset has been transformed.
In the example, I want to calculate the total and average amount of money that each function brings for the trip. What you can see in the screenshot is the result of my PARTITION BY query.
Now, if I use GROUP BY instead of PARTITION BY in the above case, what would the result look like?
First, the syntax of GROUP BY can be written as:
SELECT expression 1, expression 2aggregate function ()FROM tablesWHERE conditionsGROUP BY expression 1, expression 2
When I apply this to the query to find the total and average amount of money in each function, the aggregated output is similar to a PARTITION BY clause. However, as you notice, there is a difference in the figure 3 and figure 4 result.
GROUP BY gives per function in the company a result (Figure 4). Meanwhile, as we have 7 records in the df table, PARTITION BY retrieves all 7 rows with total_amount and average_amount in each row (Figure 3). Therefore, in conclusion, the PARTITION BY retrieves all the records in the table, while the GROUP BY only returns a limited number.
One more thing is that GROUP BY does not allow to add columns which are not parts of GROUP BY clause in select statement. However, with PARTITION BY clause, we can add required columns.
We can combine PARTITION BY and ROW NUMBER to have the row number sorted by a specific value. For example, if I want to see which person in each function brings the most amount of money, I can easily find out by applying the ROW_NUMBER function to each team and getting each person’s amount of money ordered by descending values.
PARTITION BY + ROWS UNBOUNDED PRECEDING
For easier imagination, I will begin with an example to explain the idea of this section.
I created a new table named df8.
By applying ROW_NUMBER, I got the row number value sorted by amount of money for each employee in each function. Basically until this step, as you can see in figure 7, everything is similar to the example above.
However, as I want to calculate one more column, which is the average money amount of the current row and the higher value amount before the current row in partition. For example in the figure 8, we can see that:
In the Tech team, Sam alone has an average cumulative amount of 400000.
However, in row number 2 of the Tech team, the average cumulative amount is 340050, which equals the average of (Hoang’s amount + Sam’s amount).
In row number 3, the money amount of Dung is lower than Hoang and Sam, so his average cumulative amount is average of (Hoang’s, Sam’s and Dung’s amount)
=> This is a general idea of how ROWS UNBOUNDED PRECEDING and PARTITION BY clause are used together.
PARTITION BY + ROWS BETWEEN CURRENT ROW AND 1
The usage of this combination is to calculate the aggregated values (average, sum, etc) of the current row and the following row in partition. Let’s continue to work with df9 data to see how this is done.
From figure 9, we can figure out that:
In Tech function row number 1, the average cumulative amount of Sam is 340050, which equals the average amount of her and her following person (Hoang) in row number 2.
Then, the average cumulative amount of Hoang is the average of Hoang’s amount and Dung’s amount in row number 3.
In this article, I provided my understanding of PARTITION BY and GROUP BY along with some different cases of using PARTITION BY. I hope the above information will be helpful for you.