runtime
43 / 88

task History, SCD & as-of · hard

Salary at the time of each review

For every performance review, return review_id, employee_id, review_date and salary_at_review: the salary from salary_history whose period contains the review date.

what to return

Columns
review_idemployee_idreview_datesalary_at_review in this order
Rows
13 rows · any order
Watch for
A period covers a date when valid_from ≤ date and (valid_to is NULL or date < valid_to).
If more than one period covers the date (bad data), use the one with the latest valid_from.
If no period covers it, salary_at_review is NULL — keep the review.
Skill
Join facts to the history version valid at the event date. Refresher: Protect fact grain when joining versions →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result13 rows · any order
review_idemployee_idreview_datesalary_at_review
122021-12-15165000
242022-04-01140000
352022-01-31118000

first 3 of 13 rows

data you can query

Sample rows from each table. Each column shows its type underneath; → marks a link to another table.

performance_reviews4 columns · 13 rows
idinteger · keyemployee_idintegerreview_datetextratinginteger
122021-12-154
242022-04-015
352022-01-314
452024-02-015
562023-12-153

showing 5 of 13 rows

salary_history4 columns · 21 rows
employee_idintegersalaryrealvalid_fromtextvalid_totext
21650002019-06-152022-01-01
21800002022-01-01NULL
41200002020-05-042022-04-01
41400002022-04-01NULL
51180002021-02-012022-02-01

showing 5 of 21 rows

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.