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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Optimize Complex SQL Execution
00:00
5 left

Optimize Complex SQL Execution

HardSQL · PostgreSQL

Problem

Given a C++ codebase simulating execution of a complex SQL query with multiple joins and aggregations that runs in ~20 seconds, optimize it to run below 5 seconds.

Asked in the Round 4: Query Optimization stage. For this SQL exercise, return enabled queries whose total CPU time is at least 100 milliseconds.

Output

  1. One row per qualifying query, with query_name, step_count, total_rows_processed, total_cpu_ms, and peak_memory_mb.
  2. Sort by total_cpu_ms descending, then query_id ascending for ties.

Schema

queries
ColumnTypeDescription
query_idPKINTEGERUnique simulated query identifier
query_nameVARCHAR(100)Descriptive query name
is_enabledBOOLEANWhether the query is enabled
query_steps
ColumnTypeDescription
step_idPKINTEGERUnique execution step identifier
query_idINTEGERReferenced query
step_nameVARCHAR(100)Name of the execution step
step_orderINTEGERExecution position within the query
step_metrics
ColumnTypeDescription
metric_idPKINTEGERUnique metric sample identifier
step_idINTEGERReferenced execution step
rows_processedBIGINTRows processed in the metric sample
cpu_msNUMERIC(12,2)CPU time in milliseconds
memory_mbNUMERIC(12,2)Peak memory usage in megabytes
Tablesqueriesquery_stepsstep_metrics
Interviewer

Your question is Optimize Complex SQL Execution. 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.