Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Debug and Optimize Slow SQL
00:00
5 left

Debug and Optimize Slow SQL

MediumSQL · PostgreSQL

Problem

A reporting query at a commerce platform summarizes recent orders for active customers, but the production version is slow. Write a PostgreSQL query that returns the top active customers by order revenue since January 1, 2024, while also providing an execution plan that can be inspected for scans, joins, aggregation, sorting, and buffer usage.

Output

  1. Return the PostgreSQL execution plan for the query.
  2. The underlying result contains one row per active customer with orders on or after January 1, 2024, including customer_id, order_count, and total_amount.
  3. Sort customers by total_amount descending and return at most 10 customers.

Schema

customers
ColumnTypeDescription
customer_idPKBIGINTUnique customer identifier
customer_nameVARCHAR(255)Customer display name
is_activeBOOLEANWhether the customer is currently active
signup_dateDATEDate the customer account was created
orders
ColumnTypeDescription
order_idPKBIGINTUnique order identifier
customer_idBIGINTCustomer who placed the order
order_dateDATEDate the order was placed
total_amountNUMERIC(12,2)Order total amount
Tablesorderscustomers
Interviewer

Your question is Debug and Optimize Slow SQL. 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.