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
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
Optimize Slow SQL Joins
00:00
5 left

Optimize Slow SQL Joins

MediumSQL · PostgreSQL

Problem

How do you optimize a slow-running SQL join on tables containing millions of enterprise transaction records?

Use the supplied transaction, account, and event tables. Return only positive transactions from the first quarter of 2025 whose latest event before April 1, 2025 is SETTLED.

Output

  1. One row per account and transaction month
  2. Columns: account_id, account_name, transaction_month, settled_transaction_count, and total_settled_amount
  3. Sort by account_id, then transaction_month ascending

Schema

enterprise_accounts
ColumnTypeDescription
account_idPKINTUnique enterprise account identifier
account_nameVARCHAR(120)Enterprise account name
regionVARCHAR(40)Account operating region
enterprise_transactions
ColumnTypeDescription
transaction_idPKINTUnique enterprise transaction identifier
account_idINTOwning enterprise account
transaction_dateDATEDate on which the transaction occurred
amountNUMERIC(18,2)Transaction amount in the transaction currency
currencyVARCHAR(3)ISO currency code
transaction_events
ColumnTypeDescription
event_idPKINTUnique transaction event identifier
transaction_idINTTransaction associated with the event
event_timeTIMESTAMPTime when the event was recorded
event_statusVARCHAR(20)Status recorded by the event
Tablesenterprise_accountsenterprise_transactionstransaction_events
Interviewer

Your question is Optimize Slow SQL Joins. 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.