Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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
ExperiencesCoaching
New
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
Get started
Optimize a Slow SQL Query
00:00
5 left

Optimize a Slow SQL Query

MediumSQL · PostgreSQL

Problem

Describe your process for optimizing a slow-running SQL query on Turo data.

Use PostgreSQL's pg_stat_statements view to identify the most expensive statements. The result should focus on statements with positive, non-NULL total execution time and support prioritizing optimization work.

Output

  1. Return up to 10 rows from pg_stat_statements.
  2. Include query, calls, total_exec_time, mean_exec_time, rows, shared_blks_hit, and shared_blks_read.
  3. Sort by total_exec_time descending, then query ascending. Ignore rows where total_exec_time is NULL.

Schema

pg_stat_statements
ColumnTypeDescription
queryVARCHAR(500)Normalized SQL statement text
callsBIGINTNumber of times the statement was executed
total_exec_timeNUMERIC(20,3)Total execution time in milliseconds
mean_exec_timeNUMERIC(20,3)Mean execution time per call in milliseconds
rowsBIGINTTotal rows returned or affected
shared_blks_hitBIGINTShared buffer cache hits
shared_blks_readBIGINTShared blocks read from storage
Tablespg_stat_statements
Interviewer

Your question is Optimize a Slow SQL Query. Start with the requirements and the one table 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.