Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Merchants Per City SQL

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

Your question is Top Merchants Per City SQL. 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

DoorDash wants to identify the highest-volume merchants in each city using completed orders. Write a PostgreSQL query that returns the top three merchants by total sales amount within every city.

Requirements

  1. Join orders to merchants to display merchant names.
  2. Include only orders with order_status = 'completed' and a non-null city.
  3. Aggregate sales by city and merchant, then rank merchants independently within each city.
  4. Return at most three merchants per city, ordered by city, total sales descending, and merchant name as a deterministic tie-breaker.

Schema

orders
ColumnTypeDescription
order_idPKINTUnique order identifier
merchant_idINTMerchant associated with the order
cityVARCHAR(100)Delivery city
order_statusVARCHAR(20)Order state, such as completed or cancelled
order_totalDECIMAL(10,2)Customer order amount
merchants
ColumnTypeDescription
merchant_idPKINTUnique merchant identifier
merchant_nameVARCHAR(150)Merchant display name
Tablesordersmerchants
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results