SQL Joins SQL Topic exerciseHardVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Temporal "As-Of" JOIN — Salary on a Specific Date

Join employees to salary-history intervals containing the as-of date.

  • Joins
  • Date analysis
  • NULL handling
  • Sorting

Exercise brief

Understand the request

Payroll audit lead An audit must reconstruct each employee’s salary as it stood on January 1, 2023.

Find what each employee was earning AS OF 2023-01-01: the salary_history row whose effective_from <= '2023-01-01' AND (effective_to IS NULL OR effective_to >= '2023-01-01'). INNER JOIN — employees with no qualifying history row are dropped. Return employee_id, first_name, last_name, salary, effective_from, effective_to — ordered by employee_id.

Return

  • Return employee_id, first_name, last_name, salary, effective_from, effective_to in this exact left-to-right order.

Constraints

  • Put the employee key and both effective-date predicates in ON.
  • Retain only employees with a qualifying history interval.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

employees

  • employee_idINTEGER
  • first_nameVARCHAR(50)
  • last_nameVARCHAR(50)

salary_history

  • employee_idINTEGER
  • salaryINTEGER
  • effective_fromDATE
  • effective_toDATE

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

'Active at moment T': effective_from <= T AND (effective_to IS NULL OR effective_to >= T).

Hint 2

Put the temporal predicate IN THE ON clause — it is part of the join, not a post-join filter.

Hint 3

If two rows ever overlap for the same entity, this query returns BOTH — that means your history table has a data-quality bug.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT e.employee_id, e.first_name, e.last_name, sh.salary, sh.effective_from, sh.effective_to FROM employees e INNER JOIN salary_history sh ON sh.employee_id = e.employee_id AND sh.effective_from <= '2023-01-01' AND (sh.effective_to IS NULL OR sh.effective_to >= '2023-01-01') ORDER BY e.employee_id;

Why this works

Temporal/'as-of' joins are essential in finance, audit, and any reporting that has to be reproducible at a past moment. Modern engines have specialized syntax: SQL Server's `FOR SYSTEM_TIME AS OF`, PostgreSQL with range types, etc. The classic `from <= T AND (to IS NULL OR to >= T)` predicate works on every engine.

Success check

Each employee with coverage on the as-of date appears with the correct historical salary row.

Expected result

Use this output to verify values, aliases, ordering, and row count.

employee_idfirst_namelast_namesalaryeffective_fromeffective_to
100JohnSmith1200002023-01-01NULL
101AliceJohnson750002021-03-202023-06-30
102BobWilson800002021-06-10NULL
103CarolDavis600002023-01-01NULL
104DavidBrown700002022-02-14NULL
105EmmaTaylor950002020-11-30NULL
106FrankGreen650002021-08-25NULL
107GraceWhite900002019-05-12NULL
108HenryClark550002022-07-18NULL

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.