Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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 feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Token Consumption Moving Averages
00:00
5 left

Token Consumption Moving Averages

MediumSQL · PostgreSQL

Problem

Anthropic needs a daily view of token consumption for enterprise accounts using the Claude API. Write a PostgreSQL query that aggregates usage events and calculates both short-term usage trends and cumulative consumption.

Requirements

  1. Include only accounts where account_type = 'enterprise' and status = 'active'.
  2. Aggregate multiple token usage events for the same account and date into daily_tokens. Treat NULL token counts as zero.
  3. Calculate a three-row moving average, including the current day and up to the two preceding observed usage days for that account.
  4. Calculate a cumulative token total for each account in chronological order, and sort the final output by account name and usage date.

Schema

enterprise_accounts
ColumnTypeDescription
account_idPKINTEGERUnique enterprise account identifier
account_nameVARCHAR(100)Enterprise customer name
account_typeVARCHAR(30)Customer segment
statusVARCHAR(20)Account lifecycle status
token_usage
ColumnTypeDescription
usage_idPKBIGINTUnique token usage event identifier
account_idINTEGERReferences enterprise_accounts.account_id
usage_dateDATEUTC calendar date of token usage
token_countBIGINTTokens consumed in the event
Tablesenterprise_accountstoken_usage
Interviewer

Your question is Token Consumption Moving Averages. 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.