Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Latest Passing Run Before Each Failure

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

Your question is SQL Latest Passing Run Before Each Failure. Start with the requirements and the two 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

Apex Systems QA engineers review failed executions in the Apex QA test dashboard and need the most recent successful run for comparison. Write a PostgreSQL query that returns the latest passing run before every failure for the same test case and environment.

Requirements

  1. Return every failed run, including failures with no earlier passing run.
  2. Match passing runs by test_case_id and environment, treating two NULL environments as equal.
  3. Select the passing run with the greatest finished_at before the failure, using run_id as a tie-breaker.
  4. Order the output by failure_run_id.

Schema

test_cases
ColumnTypeDescription
test_case_idPKINTEGERUnique test case identifier
test_nameVARCHAR(120)Test name shown in the Apex QA dashboard
team_nameVARCHAR(80)QA team responsible for the test
test_runs
ColumnTypeDescription
run_idPKINTEGERUnique test execution identifier
test_case_idINTEGERReferences test_cases.test_case_id
statusVARCHAR(20)Execution result
finished_atTIMESTAMPTime when the run finished
environmentVARCHAR(20)Execution environment, which may be NULL
Tablestest_casestest_runs
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results