Number rows within an ordered partition.
Section: Window Function Basics
Assign row numbers
sql
sql
SELECT order_id, customer_id, created_at,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY created_at DESC) AS rn
FROM orders;Explanation
ROW_NUMBER is useful for deduping and top-N-per-group queries.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Window Function Basics