Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Compare Two Tables in SQL

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

Your question is Compare Two Tables in SQL. 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

Persistent Systems' QA validation pipeline stores expected test results in one table and processed results in another. Write a PostgreSQL query to identify discrepancies between the two datasets.

Requirements

  1. Compare records by test_case_id using a join that preserves unmatched records from both tables.
  2. Classify each discrepancy as MISSING_SOURCE, MISSING_TARGET, or VALUE_MISMATCH.
  3. Compare both status and result_value, treating NULL and a non-NULL value as different.
  4. Return only discrepancies, ordered by test_case_id.

Schema

source_test_data
ColumnTypeDescription
test_case_idPKINTUnique expected test case identifier
statusVARCHAR(20)Expected execution status
result_valueINTExpected numeric result
target_test_data
ColumnTypeDescription
test_case_idPKINTUnique processed test case identifier
statusVARCHAR(20)Actual execution status
result_valueINTActual numeric result
Tablessource_test_datatarget_test_data
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results