Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Optimizing Slow SQL Queries
00:00
5 left

Optimizing Slow SQL Queries

MediumSQL · PostgreSQL

Problem

How would you optimize a slow-performing SQL query?

Use the supplied PostgreSQL schema and data to demonstrate an optimized reporting query. Return monthly completed transaction metrics for active accounts, include the latest review status when available, and calculate the previous reported month and percentage change.

Output

  1. One row per active account and month with completed transactions.
  2. Columns: account_id, account_name, month_start, transaction_count, total_amount, prior_month_amount, percent_change, and latest_review_status.
  3. Order by account_id, then month_start ascending. Use NULL when no prior month or review exists.

Schema

accounts
ColumnTypeDescription
account_idPKINTUnique account identifier
account_nameVARCHAR(100)Display name of the account
account_statusVARCHAR(20)Current account status
regionVARCHAR(40)Operating region
transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
account_idINTReferenced account identifier
occurred_atTIMESTAMPTransaction timestamp
statusVARCHAR(20)Transaction processing status
amountNUMERIC(14,2)Transaction amount
account_reviews
ColumnTypeDescription
review_idPKINTUnique review identifier
account_idINTReviewed account identifier
reviewed_atTIMESTAMPReview completion timestamp
review_statusVARCHAR(20)Outcome of the review
Tablesaccountstransactionsaccount_reviews
Interviewer

Your question is Optimizing Slow SQL Queries. 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.