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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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 Multi-Join SQL
00:00
5 left

Optimize Slow Multi-Join SQL

HardSQL · PostgreSQL

Problem

How would you optimize a slow-running SQL query that joins multiple large datasets?

Using the provided PostgreSQL schema, write an optimized query that returns completed transfer performance for the first quarter of 2025. The answer should also make clear which query-planning and indexing decisions support performance at scale.

Output

  1. One row per corridor, calendar month, and customer segment.
  2. Columns: corridor_code, month_start, customer_segment, completed_transfer_count, completed_amount, and average_transfer_amount.
  3. Include only transfers whose latest event is completed, excluding unmatched customers or corridors.
  4. Order by corridor_code, month_start, and customer_segment, with NULL segments last.

Schema

transfers
ColumnTypeDescription
transfer_idPKINTUnique transfer identifier
customer_idINTCustomer who initiated the transfer
corridor_idINTTransfer corridor
created_atTIMESTAMPTransfer creation timestamp
amountNUMERIC(12,2)Transfer amount
transfer_events
ColumnTypeDescription
event_idPKINTUnique event identifier
transfer_idINTTransfer associated with the event
statusVARCHAR(20)Status recorded by the event
event_atTIMESTAMPEvent timestamp
customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_segmentVARCHAR(30)Customer segment
corridors
ColumnTypeDescription
corridor_idPKINTUnique corridor identifier
corridor_codeVARCHAR(20)Origin and destination corridor code
Tablestransferstransfer_eventscustomerscorridors
Interviewer

Your question is Optimize Slow Multi-Join SQL. Start with the requirements and the four 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.