Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Cities by Ride Growth

MediumSQL · PostgreSQL00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the SQL screen.

The question is on your right: Top Cities by Ride Growth. Read through the requirements and the three tables first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

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

Problem

SwiftRide wants to identify which cities are growing fastest in completed rides month over month. Write a SQL query to return the top cities by ride growth rate for each month.

Requirements

  1. Count completed rides per city per month.
  2. Compare each city-month to the previous month for the same city.
  3. Calculate growth rate as (current_month_rides - previous_month_rides) / previous_month_rides * 100.
  4. Return only months where the previous month exists and previous month rides are greater than 0.
  5. For each month, rank cities by growth rate descending and return the top 2 cities.

Schema

cities
ColumnTypeDescription
city_idPKINTUnique city identifier
city_nameVARCHAR(100)City name
regionVARCHAR(50)Operating region for the city
drivers
ColumnTypeDescription
driver_idPKINTUnique driver identifier
city_idINTDriver's assigned city
driver_nameVARCHAR(100)Driver full name
statusVARCHAR(20)Driver status such as active or inactive
rides
ColumnTypeDescription
ride_idPKINTUnique ride identifier
driver_idINTDriver associated with the ride
rider_idINTRider identifier
ride_dateDATEDate the ride occurred
ride_statusVARCHAR(20)Ride status such as completed or cancelled
fare_amountNUMERIC(10,2)Fare charged for the ride
Tablescitiesdriversrides
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results