Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Optimizing Queries at Scale
00:00
5 left

Optimizing Queries at Scale

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.

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

Your question is Optimizing Queries at Scale. 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.