+2 votes
in Sql by
What is the difference between PARTITION BY and GROUP BY

1 Answer

0 votes
by

They're used in different places. group by modifies the entire query, like:

select customerId, count(*) as orderCount
from Orders
group by customerId

But partition by just works on a window function, like row_number:

select row_number() over (partition by customerId order by orderId)
    as OrderNumberForThisCustomer
from Orders

A group by normally reduces the number of rows returned by rolling them up and calculating averages or sums for each row. partition by does not affect the number of rows returned, but it changes how a window function's result is calculated.

Related questions

+2 votes
asked Jan 15, 2022 in Sql by GeorgeBell
+2 votes
0 votes
+1 vote
asked Jan 15, 2022 in Sql by GeorgeBell
...