sqlaggregate-functionswindow-functions

What is the difference between PARTITION BY and GROUP BY


I've been using GROUP BY for all types of aggregate queries over the years. Recently, I've been reverse-engineering some code that uses PARTITION BY to perform aggregations.

In reading through all the documentation I can find about PARTITION BY, it sounds a lot like GROUP BY, maybe with a little extra functionality added in.

Are they two versions of the same general functionality or are they something different entirely?


Solution

  • 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