runtime
44 / 88

task Validation & types · hard

Detect overlapping history periods

Within one employee's salary_history, periods must not overlap. Return employee_id, first_from and second_from for every pair of overlapping periods, where first_from is the earlier valid_from.

what to return

Columns
employee_idfirst_fromsecond_from in this order
Rows
2 rows · any order
Watch for
A period runs from valid_from up to but not including valid_to; NULL valid_to means it is still open.
Periods that touch (one ends the day the next starts) do not overlap.
Report each overlapping pair once.
Skill
Validate that history intervals don't overlap. Refresher: Self-joins & referral relationships →

expected result

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

expected result2 rows · any order
employee_idfirst_fromsecond_from
62022-07-182023-11-01
132023-01-012024-06-15

all 2 rows

data you can query

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

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.