Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.