Delta From Starting Salary (FIRST_VALUE)
For each salaries_history record, show how far the salary has moved from the employee's very first recorded salary. Return employee_id, effective_date, salary, starting_salary, delta_from_start — ordered by employee_id, effective_date.
- Window functions
- Sorting
Interview brief
Understand the request
For each salaries_history record, show how far the salary has moved from the employee's very first recorded salary. Return employee_id, effective_date, salary, starting_salary, delta_from_start — ordered by employee_id, effective_date.
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
salaries_history
employee_idINTEGERsalaryINTEGEReffective_dateDATE
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
FIRST_VALUE is safe with the default frame (the partition start never moves).
Hint 2
delta_from_start = current salary − first salary; the first row is always 0.
Hint 3
This is cohort analysis: every record measured against a fixed baseline.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT employee_id, effective_date, salary, FIRST_VALUE(salary) OVER (PARTITION BY employee_id ORDER BY effective_date) AS starting_salary, salary - FIRST_VALUE(salary) OVER (PARTITION BY employee_id ORDER BY effective_date) AS delta_from_start FROM salaries_history ORDER BY employee_id, effective_date;Why this works
Indexing every row to a fixed baseline (the first value) is the heart of cohort and 'growth since start' analysis. Unlike LAST_VALUE, FIRST_VALUE works with the default frame because the partition's start is always in view.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| employee_id | effective_date | salary | starting_salary | delta_from_start |
|---|---|---|---|---|
| 4 | 2020-02-14 | 110000 | 110000 | 0 |
| 4 | 2021-02-14 | 120000 | 110000 | 10000 |
| 4 | 2022-02-14 | 130000 | 110000 | 20000 |
| 5 | 2020-05-18 | 105000 | 105000 | 0 |
| 5 | 2021-05-18 | 115000 | 105000 | 10000 |
| 5 | 2022-05-18 | 125000 | 105000 | 20000 |
| 6 | 2021-01-10 | 85000 | 85000 | 0 |
| 6 | 2022-01-10 | 95000 | 85000 | 10000 |
| 8 | 2020-08-15 | 95000 | 95000 | 0 |
| 8 | 2021-08-15 | 105000 | 95000 | 10000 |
Previewing 10 of 13 expected rows. Run the query in the editor to inspect the full result.
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics: