Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
ExperiencesCoaching
New
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
Start free
Window Functions for Running Totals
00:00
5 left

Window Functions for Running Totals

MediumSQL · PostgreSQL

Problem

A company 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.

Output details

  • Return columns in this order: user_id, user_name, cohort_month, activity_month, monthly_spend, user_total_spend, running_total, cohort_rank.
  • Order rows by cohort_month ascending, activity_month ascending, then user_id ascending.
  • Rank users by user_total_spend descending, place NULL totals last, and assign identical ranks to ties.
  • Preserve NULL aggregate amounts as NULL; do not convert them to 0.

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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.