Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
Start free
Tune Candidate Assessment Performance Query
00:00
5 left

Tune Candidate Assessment Performance 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.

Output details

  • Treat 2025-01-01 as an inclusive lower bound for completed_at.
  • When completion timestamps tie, select the attempt with the greatest attempt_id as latest.
  • Calculate previous_score from the preceding qualifying completed attempt ordered by completed_at ascending, then attempt_id ascending.
  • Set score_change to NULL whenever score or previous_score is NULL.
  • Sort candidate_name and assessment_name ascending after sorting completed_at descending.
  • Treat a NULL aggregate event duration as 0.

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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the four tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.