This is a typical problem called greatest-N-per-group. This normally isn't solved using order by + limit
(unless you use LATERAL
which is more complicated in my opinion), since as you've mentioned it is an answer to problem of greatest-N but not per group. In your case movie genre is the group.
You could use dense_rank
window function to generate ranks based on rating for each genre in a subquery and then select those which are top 10:
select title, rating
from (
select title, rating, dense_rank() over (partition by genre order by rating desc) as rn
from yourtable
) t
where rn <= 10
This may return more than 10 titles for each genre, because there may be ties (the same rating for different movies belonging to one genre). If you only want top 10 without looking at ties, use row_number
instead of dense_rank
.