SQL: difference between PARTITION BY and GROUP 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
  • 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.

Leave a Comment