SQL 2023 · Aggregation and window functions

Number rows within groups

Number each user's orders from newest to oldest. Filter on this to get the latest per user.

SELECT *, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY created_at DESC) AS rn FROM orders;

Official SQL 2023 documentation

More aggregation and window functions commands

All aggregation and window functions commands · SQL 2023 cheatsheet

Was this page helpful?