Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Optimize Slow Query on Transactions

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Optimize Slow Query on Transactions. Start with the requirements and the four tables on the right.

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.

Problem

How do you optimize a query that is running slowly on a massive financial transaction dataset?

Using the supplied PostgreSQL schema, write an efficient query that summarizes posted transactions during the first quarter of 2025. Include only active accounts with matching customers and merchants.

Output

  1. One row per customer, merchant category, and month
  2. Columns: customer_name, category, month_start, transaction_count, total_amount, prior_month_total
  3. Sort by customer, category, and month ascending

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
accounts
ColumnTypeDescription
account_idPKINTUnique account identifier
customer_idINTOwning customer identifier
account_statusVARCHAR(20)Current account status
merchants
ColumnTypeDescription
merchant_idPKINTUnique merchant identifier
merchant_nameVARCHAR(100)Merchant name
categoryVARCHAR(50)Merchant spending category
financial_transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
account_idINTAccount associated with the transaction
merchant_idINTMerchant associated with the transaction
occurred_atTIMESTAMPTransaction timestamp
amountNUMERIC(14,2)Signed transaction amount
statusVARCHAR(20)Transaction processing status
transaction_typeVARCHAR(30)Transaction classification
Tablescustomersaccountsmerchantsfinancial_transactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results