Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Find Most Recent Record Per Test Case
00:00
5 left

SQL Find Most Recent Record Per Test Case

MediumSQL · PostgreSQL

Problem

Write a SQL query to find the most recent record for each test case and environment.

Use the provided test_cases and test_records tables. Treat records with the same test case and environment as one group, and use record_id as the final tie-breaker when timestamps match.

Output

  1. One row per test case and environment with a matching record
  2. Columns: test_case_id, test_case_name, environment, record_id, executed_at, and status
  3. Sort by test_case_id, environment with NULL environments first, then newest timestamp and highest record ID

Schema

test_cases
ColumnTypeDescription
test_case_idPKINTUnique identifier for a test case
test_case_nameVARCHAR(150)Readable test case name
team_nameVARCHAR(100)Team responsible for the test case
test_records
ColumnTypeDescription
record_idPKINTUnique execution record identifier
test_case_idINTReferenced test case identifier
environmentVARCHAR(50)Execution environment
executed_atTIMESTAMPExecution timestamp
statusVARCHAR(20)Execution outcome
result_detailTEXTOptional execution detail
Tablestest_casestest_records
Interviewer

Your question is SQL Find Most Recent Record Per Test Case. Start with the requirements and the two 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.