Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Joins for Retention Rates
00:00
5 left

SQL Joins for Retention Rates

HardSQL · PostgreSQL

Problem

Zomato wants to measure whether customers place additional delivered orders after their first qualifying order. Write a PostgreSQL query using customers, orders, and restaurants to calculate retention by first-order month and restaurant city.

Requirements

  1. Consider only orders with status = 'delivered' placed at active restaurants.
  2. Assign each customer to the month and city of their first qualifying order.
  3. Calculate retention for months 0, 1, and 2 after the cohort month, counting each customer at most once per month.
  4. Return cohort month, city, cohort size, retained customers, and retention percentage, ordered chronologically by cohort and month offset.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique Zomato customer identifier
customer_nameVARCHAR(100)Customer display name
signup_dateDATEDate the customer registered
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer who placed the order
restaurant_idINTRestaurant receiving the order
order_dateDATEDate on which the order was placed
statusVARCHAR(20)Order lifecycle status
restaurants
ColumnTypeDescription
restaurant_idPKINTUnique restaurant identifier
restaurant_nameVARCHAR(150)Restaurant name shown on Zomato
cityVARCHAR(50)Restaurant operating city
is_activeBOOLEANWhether the restaurant is currently active
Tablescustomersordersrestaurants
Interviewer

Your question is SQL Joins for Retention Rates. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.