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

Optimizing a Slow SQL Query

MediumSQL · PostgreSQL

Problem

Describe a scenario where you had to optimize a slow-running SQL query and how you approached it.

For a hands-on PostgreSQL response, use the provided customers and orders tables to produce the optimized result described below.

Output

  1. One row per customer region and calendar month
  2. Columns: region, month, and completed_revenue
  3. Include completed orders from January 1, 2025 through March 31, 2025, excluding unmatched customers
  4. Sort by region ascending, with NULL regions first, then month ascending

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
regionVARCHAR(50)Customer geographic region
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer associated with the order
order_dateDATEOrder creation date
statusVARCHAR(20)Order processing status
amountNUMERIC(12,2)Order revenue amount
Tablescustomersorders
Interviewer

Your question is Optimizing a Slow SQL Query. 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.