Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Optimizing Complex SQL Joins

HardSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Optimizing Complex SQL Joins. Start with the requirements and the five tables on the right.

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.

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results