Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Optimize Slow SQL on Large DB
00:00
5 left

Optimize Slow SQL on Large DB

MediumSQL · PostgreSQL

Problem

How do you optimize a SQL query that is running slowly on a very large database?

Use the provided PostgreSQL schema as the working example. Produce the result for completed orders from the first quarter of 2025, and explain how you would investigate and improve performance at scale.

Output

  1. One row per customer with qualifying orders.
  2. Columns: customer_id, customer_name, region, order_count, and total_revenue.
  3. Include only completed orders dated from 2025-01-01 through 2025-03-31, with revenue above 500.
  4. Sort by total_revenue descending, then customer_id ascending.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer name
regionVARCHAR(50)Customer region
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer who placed the order
order_dateDATEDate the order was placed
statusVARCHAR(20)Order processing status
order_items
ColumnTypeDescription
order_item_idPKINTUnique order line identifier
order_idINTOrder containing the item
quantityINTNumber of units ordered
unit_priceNUMERIC(10,2)Price per unit
Tablescustomersordersorder_items
Interviewer

Your question is Optimize Slow SQL on Large DB. 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.