Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Window Functions for Running Totals
00:00
5 left

Window Functions for Running Totals

MediumSQL · PostgreSQL

Problem

NewMarket wants to evaluate user engagement by signup cohort. Write a PostgreSQL query that summarizes qualifying transactions by user and calendar month, calculates each user's cumulative spend, and ranks users within their signup cohort.

Requirements

  1. Derive cohort_month from users.signup_date and activity_month from transactions.transaction_date.
  2. Include only transactions on or after the user's signup date. Exclude transactions that cannot be matched to a user.
  3. Aggregate transaction amounts by user and activity month.
  4. Calculate a per-user running total ordered by activity month, and rank users within each cohort by total spend descending. Preserve ties with the same rank.
  5. Return one row per user and activity month, ordered by cohort, activity month, and user ID.

Schema

users
ColumnTypeDescription
user_idPKINTUnique NewMarket user identifier
user_nameVARCHAR(100)User display name
signup_dateDATEDate the user joined NewMarket
transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
user_idINTUser associated with the transaction
transaction_dateDATEDate of the transaction
amountNUMERIC(10,2)Transaction amount
Tablesuserstransactions
Interviewer

Your question is Window Functions for Running Totals. Start with the requirements and the two 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.