#188

Top 5 Merchants by Average Rating

MEDIUMAggregationPOSTGRESQL

Problem Description

You are building a merchant quality leaderboard pipeline for a food delivery platform. Given a `merchants` table and an `orders` table that captures post-delivery ratings, identify the top 5 merchants by average customer rating. **Qualification Rules:** - Only consider orders where `status = 'delivered'` — cancelled, pending, or in-progress orders are excluded - Only consider orders where `rating IS NOT NULL` — not every delivery receives a rating - Average rating is computed per merchant across all qualifying orders **Output:** Return `merchant_name` (the merchant's display name) and `avg_rating` (rounded to 2 decimal places), ordered by `avg_rating` descending. For ties, break by `merchant_name` ascending (alphabetical). Return at most 5 rows.

Topics

aggregationgroup byjoinsranking

Asked at Companies

doordash

Solve This Problem

Sign up to access the interactive code editor, run test cases, view the editorial, and get AI-powered feedback on your solution.

Start Solving →