Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 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
Build my plan
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
Sign up free to run your codeLog inLn 1
Run your query to see results here.