Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 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
Build my plan
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
Sign up free to run your codeLog inLn 1
Run your query to see results here.