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
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
Get started
Optimizing Billion Row Monthly Event Reports
00:00
5 left

Optimizing Billion Row Monthly Event Reports

HardSQL · PostgreSQL

Problem

How do you optimize query performance on a database containing billions of rows of historical data?

Use the provided PostgreSQL schema to produce an efficient report for completed events during 2025. The result should aggregate monthly activity for active accounts and calculate each account and event type's change from the previous month.

Output

  1. One row per active account, event type, and month with qualifying activity.
  2. Columns: account_name, region, event_name, month_start, event_count, total_amount, prior_month_amount, and amount_change.
  3. Sort by account, event type, and month in ascending order.

Output details

  • Treat 2025 as the half-open interval from 2025-01-01 inclusive through 2026-01-01 exclusive.
  • Sort ascending by account_id, then event_type_id, then month_start, not by the displayed names.
  • Preserve NULL region values.
  • Leave prior_month_amount NULL for each account and event type's first observed month.
  • Treat a missing prior amount as numeric zero when calculating amount_change.
  • Treat an all-NULL monthly amount sum as numeric zero without rounding.

Schema

historical_events
ColumnTypeDescription
event_idPKBIGINTUnique historical event identifier
account_idBIGINTAccount associated with the event
event_type_idINTType of historical event
occurred_atDATEDate when the event occurred
amountNUMERIC(12,2)Financial amount associated with the event
statusVARCHAR(20)Processing status of the event
accounts
ColumnTypeDescription
account_idPKBIGINTUnique account identifier
account_nameVARCHAR(100)Display name of the account
regionVARCHAR(50)Operating region of the account
is_activeBOOLEANWhether the account is currently active
event_types
ColumnTypeDescription
event_type_idPKINTUnique event type identifier
event_nameVARCHAR(80)Human-readable event type name
Tableshistorical_eventsaccountsevent_types
Interviewer

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

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.