Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Optimize Slow Client Query
00:00
5 left

Optimize Slow Client Query

MediumSQL · PostgreSQL

Problem

How would you optimize a database query that is performing slowly for a client with millions of records?

Using the provided PostgreSQL schema, return the latest completed attempt for each active candidate and active assessment since 2025-01-01. Include prior score information and recorded event metrics so the query can be evaluated for correctness and performance.

Output

  1. One row per candidate and assessment combination with a qualifying completed attempt.
  2. Columns: candidate_name, assessment_name, completed_at, score, previous_score, score_change, event_count, and total_duration_seconds.
  3. Include candidates with no matching events, treating their event count and duration as zero.
  4. Sort by completed_at descending, then candidate_name, then assessment_name.

Schema

candidates
ColumnTypeDescription
candidate_idPKINTUnique candidate identifier
candidate_nameVARCHAR(100)Candidate display name
statusVARCHAR(20)Candidate lifecycle status
assessments
ColumnTypeDescription
assessment_idPKINTUnique assessment identifier
assessment_nameVARCHAR(150)Assessment title
statusVARCHAR(20)Assessment lifecycle status
attempts
ColumnTypeDescription
attempt_idPKINTUnique attempt identifier
candidate_idINTCandidate who made the attempt
assessment_idINTAssessment being attempted
statusVARCHAR(20)Attempt status
completed_atTIMESTAMPCompletion timestamp
scoreINTAssessment score
attempt_events
ColumnTypeDescription
event_idPKINTUnique event identifier
attempt_idINTAttempt associated with the event
event_typeVARCHAR(30)Type of activity event
duration_secondsINTEvent duration in seconds
Tablescandidatesassessmentsattemptsattempt_events
Interviewer

Your question is Optimize Slow Client Query. Start with the requirements and the four 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.