Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Optimize Slow Query or Pipeline
00:00
5 left

Optimize Slow Query or Pipeline

MediumSQL · PostgreSQL

Problem

Given a dataset, how would you optimize a slow-running query or pipeline?

Use the provided dataset to demonstrate an optimized PostgreSQL query that calculates completed event activity for active accounts during January 2025. The query should avoid unnecessary rows and preserve accounts whose qualifying events have no item value.

Output

  1. Return at most five rows with account_id, account_name, event_count, and total_value.
  2. Include only active accounts with completed January events.
  3. Order by total_value descending, then account_id ascending.

Schema

accounts
ColumnTypeDescription
account_idPKINTUnique account identifier
account_nameVARCHAR(100)Display name of the account
regionVARCHAR(50)Account region
is_activeBOOLEANWhether the account is active
events
ColumnTypeDescription
event_idPKINTUnique event identifier
account_idINTAccount associated with the event
event_tsTIMESTAMPEvent timestamp
statusVARCHAR(20)Event processing status
event_items
ColumnTypeDescription
item_idPKINTUnique event item identifier
event_idINTEvent containing the item
quantityINTNumber of units
unit_priceDECIMAL(10,2)Price per unit
Tablesaccountseventsevent_items
Interviewer

Your question is Optimize Slow Query or Pipeline. 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.