Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Three Businesses Per City

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Top Three Businesses Per City. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Yelp wants to identify the most reviewed businesses in each city for local discovery and marketplace analysis. Write a PostgreSQL query that ranks every business within its city by the number of reviews it has received.

Requirements

  1. Count reviews for each business, including businesses with zero reviews.
  2. Rank businesses separately within each city, ordering by review count descending and business ID ascending to break ties.
  3. Return only the top three businesses per city.
  4. Include the city, business ID, business name, review count, and rank in the result.

Schema

businesses
ColumnTypeDescription
business_idPKINTUnique Yelp business identifier
business_nameVARCHAR(120)Display name of the business
cityVARCHAR(80)City where the business is located
reviews
ColumnTypeDescription
review_idPKINTUnique review identifier
business_idINTBusiness receiving the review
reviewer_nameVARCHAR(100)Yelp member who wrote the review
review_textTEXTReview content
Tablesbusinessesreviews
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results