SQL: with "ratings" as (select "public"."book"."genre", "public"."book"."title", avg(cast("public"."review"."rating" as double precision)) as "avg_rating" from "public"."book" join "public"."review" on "public"."review"."book_id" = "public"."book"."id" group by "public"."book"."genre", "public"."book"."title"), "ranked" as (select "ratings"."genre", "ratings"."title", "ratings"."avg_rating", rank() over (partition by "ratings"."genre" order by "ratings"."avg_rating" desc, "ratings"."title") as "rnk" from "ratings") select "ranked"."genre", "ranked"."title", "ranked"."avg_rating", "ranked"."rnk" from "ranked" where "ranked"."rnk" = ? order by "ranked"."genre" GenreLeader[genre=FICTION, title=Poetical Science, avgRating=3.0, rank=1] GenreLeader[genre=SCIENCE, title=Notes on the Analytical Engine, avgRating=4.5, rank=1] GenreLeader[genre=HISTORY, title=Tokyo Trains, avgRating=4.0, rank=1] GenreLeader[genre=TECH, title=Compilers for Humans, avgRating=4.666666666666667, rank=1] SQL: select "public"."author"."name", count(*), sum(("public"."book"."price" * "public"."book"."stock")) from "public"."author" join "public"."book" on "public"."book"."author_id" = "public"."author"."id" group by "public"."author"."name" order by sum(("public"."book"."price" * "public"."book"."stock")) desc LeaderboardRow[author=Kenji Tanaka, books=2, catalogueValue=544.80] LeaderboardRow[author=Grace Hopper, books=2, catalogueValue=315.00] LeaderboardRow[author=Ada Lovelace, books=2, catalogueValue=305.00]