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
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
Sign up free to run your codeLog inLn 1
Run your query to see results here.