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
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
Get started
Latest Record Per Key
00:00
5 left

Latest Record Per Key

MediumSQL · PostgreSQL

Problem

Apex Systems QA engineers use a test execution table to track results from automated and manual validation runs. Write a PostgreSQL query that returns the latest execution for every active test case.

Requirements

  1. Use a common table expression and ROW_NUMBER() to rank executions separately for each test_case_id.
  2. Define the latest execution by greatest executed_at. If timestamps tie, use the greatest execution_id as the deterministic tie-breaker.
  3. Return only the most recent execution for active test cases. Test cases without executions and inactive test cases must not appear.
  4. Return the test case ID, test case name, latest status, execution environment, and execution timestamp, ordered by test case ID.

Schema

test_cases
ColumnTypeDescription
test_case_idPKINTEGERUnique test case identifier
test_case_nameVARCHAR(150)Descriptive test case name
is_activeBOOLEANWhether the test case is currently active
test_executions
ColumnTypeDescription
execution_idPKINTEGERUnique execution identifier
test_case_idINTEGERReferenced test case
statusVARCHAR(20)Execution result
environmentVARCHAR(30)Execution environment
executed_atTIMESTAMPTZExecution completion timestamp
tester_emailVARCHAR(150)QA engineer or automation account
Tablestest_casestest_executions
Interviewer

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

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.