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

Optimizing Slow SQL or Spark

MediumSQL · PostgreSQL

Problem

Describe a time you had to optimize a slow-running SQL query or Spark job.

Using the provided PostgreSQL tables, rewrite the slow SQL pattern to return the required results efficiently. Explain how you would validate the improvement and identify useful indexes.

Output

  1. One row per active store, including stores with no qualifying transactions.
  2. Columns: store_id, store_name, completed_transaction_count, and completed_revenue.
  3. Include completed transactions from January 1, 2025 through March 31, 2025, sort by revenue descending, then store_id ascending.

Schema

stores
ColumnTypeDescription
store_idPKINTUnique store identifier
store_nameVARCHAR(100)Store display name
regionVARCHAR(50)Geographic region
is_activeBOOLEANWhether the store is currently active
transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
store_idINTStore associated with the transaction
transaction_dateDATETransaction date
statusVARCHAR(20)Transaction processing status
amountNUMERIC(10,2)Transaction amount
Tablesstorestransactions
Interviewer

Your question is Optimizing Slow SQL or Spark. Start with the requirements and the two 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.