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
Optimizing Complex SQL Joins
00:00
5 left

Optimizing Complex SQL Joins

HardSQL · PostgreSQL

Problem

How would you optimize a slow-running SQL query that involves multiple heavy joins and aggregations on partitioned tables?

Use the supplied project, event, cost, and assignment tables to produce a representative optimized report for active projects overlapping January and February 2025. Preserve active projects that have no matching events or costs.

Output

  1. One row per qualifying project with client_name, project_id, billable_hours, revenue, total_cost, and consultant_count.
  2. Include projects active during the period, order by revenue descending, then project_id ascending.

Schema

clients
ColumnTypeDescription
client_idPKINTUnique client identifier
client_nameVARCHAR(100)Client display name
projects
ColumnTypeDescription
project_idPKINTUnique project identifier
client_idINTOwning client
project_nameVARCHAR(120)Project name
statusVARCHAR(20)Project lifecycle status
start_dateDATEProject start date
end_dateDATEProject end date, if completed
project_events
ColumnTypeDescription
event_idPKINTUnique project event identifier
project_idINTRelated project
event_dateDATEEvent date and partitioning key
event_typeVARCHAR(40)Type of project activity
hoursNUMERIC(10,2)Hours recorded for the event
billableBOOLEANWhether the hours are billable
billing_rateNUMERIC(10,2)Hourly billing rate
project_costs
ColumnTypeDescription
cost_idPKINTUnique cost identifier
project_idINTRelated project
cost_dateDATECost date and partitioning key
categoryVARCHAR(50)Cost category
amountNUMERIC(12,2)Cost amount
project_assignments
ColumnTypeDescription
assignment_idPKINTUnique assignment identifier
project_idINTRelated project
consultant_idINTConsultant identifier
consultant_nameVARCHAR(100)Consultant name
Tablesclientsprojectsproject_eventsproject_costsproject_assignments
Interviewer

Your question is Optimizing Complex SQL Joins. Start with the requirements and the five 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.