Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL for Monthly Aggregates
00:00
5 left

SQL for Monthly Aggregates

HardSQL · PostgreSQL

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
Interviewer

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