Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

First and Last Result per Case

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

Your question is First and Last Result per Case. 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

Write a SQL query to find the first and last test result for each test case and environment.

Use the test_cases and test_results tables. The first result is determined by the earliest executed_at; if timestamps tie, use the smaller result_id first.

Output

  1. One row per test case and environment with at least one result.
  2. Columns: test_case_id, test_case_name, environment, first_result, and last_result.
  3. Sort by test_case_id, then environment.

Schema

test_cases
ColumnTypeDescription
test_case_idPKINTUnique identifier for the test case
test_case_nameVARCHAR(150)Descriptive name of the test case
test_suiteVARCHAR(100)Name of the suite containing the test case
test_results
ColumnTypeDescription
result_idPKINTUnique identifier for the test result
test_case_idINTReferenced test case
environmentVARCHAR(50)Environment where the test ran
result_statusVARCHAR(30)Outcome recorded for the test execution
executed_atTIMESTAMPTime when the test execution completed
Tablestest_casestest_results
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results