How do you get the top N rows per group?
Model answer
Number the rows within each group using ROW_NUMBER() OVER (PARTITION BY group ORDER BY value DESC) in a subquery or CTE, then filter where the number is <= N.
Learn this topic
GROUP BY and HAVING: Summaries for Each Group in the course SQL for Beginners: Learn Databases Step by Step.
Sign in to add your own answer.