Top Rated Recipes

ASKED IN INTERVIEW
4pts
Pinterest

Problem Statement

Pinterest users want to see the best recipes for their specific diets. Find the highest-rated recipe in each category.

Rules:

  • Output columns: category, recipe_name, avg_rating.
  • Calculate average rating per recipe first.
  • In case of a tie for the top spot in a category, return all tied recipes.
  • Round average ratings to 2 decimal places.
  • Results must be sorted by category in ascending order, then by avg_rating in descending order.
Tests your understanding of
Window Functions, Ranking and Subqueries

Input Tables

categories
category_id(INTEGER)category_name(VARCHAR)
1Vegan
2Keto
3Paleo
recipes
recipe_id(INTEGER)recipe_name(VARCHAR)category_id(INTEGER)
10Tofu Scramble1
11Chickpea Curry1
20Steak & Butter2
21Egg Salad2
30Nut Mix3
reviews
review_id(INTEGER)recipe_id(INTEGER)rating(INTEGER)
1105
2104
3115
4205
5215
6303

Expected Output

category(VARCHAR)recipe_name(VARCHAR)avg_rating(DECIMAL)
KetoEgg Salad5
KetoSteak & Butter5
PaleoNut Mix3
VeganChickpea Curry5

Tags

MediumASKED IN INTERVIEWWindow FunctionsRankingSubqueries
20-25 min
45%

Hints