Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Cohort Metrics Query

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

Your question is SQL Cohort Metrics Query. Start with the requirements and the three 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

PlayStation Network analysts want to compare engagement and purchase behavior across monthly user signup cohorts. Write a PostgreSQL query that reports activity and revenue metrics for each cohort month and subsequent active month.

Requirements

  1. Use the user's signup month as cohort_month and calculate the number of months since signup as month_number.
  2. Include active users, PlayStation 5 sessions, completed purchasers, and completed purchase revenue for each cohort-month combination. Only sessions with platform = 'PS5' and purchases with status = 'completed' count.
  3. Include cohort size, retention percentage, and average completed revenue per active user. Return rows ordered by cohort month and month number.

Representative Data

users: (1, 'Aiko Tanaka', '2025-01-05'), (3, 'Maya Singh', '2025-02-01'), (5, 'Leo Martin', '2025-03-03'), (10, 'Nora Evans', '2025-04-15')

sessions: (101, 1, '2025-01-06', 'PS5'), (102, 1, '2025-01-20', 'PS5'), (106, 3, '2025-03-05', 'PS5'), (109, 6, '2025-03-30', NULL)

purchases: (201, 1, '2025-01-06', 59.99, 'completed'), (204, 3, '2025-02-02', 69.99, 'completed'), (205, 3, '2025-03-05', 69.99, 'refunded'), (210, 10, '2025-04-16', 15.00, NULL)

Schema

users
ColumnTypeDescription
user_idPKINTPlayStation Network user identifier
display_nameVARCHAR(100)PlayStation Network display name
signup_dateDATEAccount signup date
sessions
ColumnTypeDescription
session_idPKINTGameplay or PlayStation Network session identifier
user_idINTUser who initiated the session
session_dateDATEDate of the session
platformVARCHAR(20)PlayStation platform used
purchases
ColumnTypeDescription
purchase_idPKINTPurchase identifier
user_idINTPurchasing user
purchase_dateDATEDate of purchase
amountNUMERIC(10,2)Purchase amount
statusVARCHAR(20)Purchase processing status
Tablesuserssessionspurchases
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results