Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Monthly Aggregates

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

Your question is SQL for Monthly Aggregates. 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

DigitalOcean's Financial Analytics team needs a monthly view of customer satisfaction for active paid accounts. Write a PostgreSQL query for January through April 2025.

Requirements

  1. Return one row for every calendar month in the reporting period, including months with no valid responses.
  2. Include only customers with an active paid subscription during each month and exclude internal accounts.
  3. For each customer and month, retain only the most recent CSAT response.
  4. Exclude NULL and out-of-range scores, where valid CSAT scores are integers from 1 through 5.
  5. Return the monthly average score, the previous month's average, and the month-over-month change. Round score values to two decimal places.
  6. Sort results chronologically.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(120)Customer account name
segmentVARCHAR(30)Customer business segment
is_internalBOOLEANWhether the account is an internal DigitalOcean account
subscriptions
ColumnTypeDescription
subscription_idPKINTUnique subscription identifier
customer_idINTReferenced customer
plan_nameVARCHAR(60)DigitalOcean subscription plan
started_atDATESubscription start date
ended_atDATESubscription end date, if ended
csat_responses
ColumnTypeDescription
response_idPKINTUnique survey response identifier
customer_idINTCustomer submitting the response
submitted_atTIMESTAMPResponse submission timestamp
scoreINTCustomer satisfaction score from 1 through 5
response_channelVARCHAR(30)Channel used to submit the response
Tablescustomerssubscriptionscsat_responses
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results