HardSQL challenge #386WINDOW FUNCTIONLAGCALCULATION

Salary % vs Previous Hire

Express each employee's salary as a percentage change from the salary of the employee hired just before them.

The challenge runs on the SQLabHub practice database (SQLite) and uses the employees table. You can solve it in the browser, with nothing to install.

Solve This SQL Challenge

The task

Express each employee's salary as a percentage change from the salary of the employee hired just before them.

A correct answer returns 120 rows with 2 columns: id, pct_diff.

Tables used in this challenge

employees 120 rows

id INTEGER (PK) · first_name TEXT · last_name TEXT · email TEXT · department TEXT · department_id INTEGER · title TEXT · salary REAL · hire_date TEXT · manager_id INTEGER · city TEXT · status TEXT

First 3 rows of the employees table
idfirst_namelast_nameemaildepartmentdepartment_idtitlesalaryhire_datemanager_idcitystatus
1WilliamLewiswlewis18@corp.ioCustomer Success7CS Specialist617742022-07-2460San Diegoactive
2JohnMillerjmiller51@corp.ioLegal8Senior Counsel1916942021-08-0955San Diegoactive
3DanielRiveradrivera50@corp.ioEngineering1Staff Engineer1350672016-06-06106Houstonactive

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
idpct_diff
100291.0723870868798
110264.7391431069659
41196.02834234570204
76144.77164416203337
2132.01888162672478

First 5 of 120 rows. This is the output of the model solution on the practice database.

What you practise

  • Reading the previous row's value with LAG()
  • Sorting the result with ORDER BY
  • Deriving a new column from an expression and naming it with an alias

Hint and approach

Try the challenge first. Open these only when you need them.

Show the hint

LAG(salary) ordered by hire_date, then (salary - previous) * 100.0 / previous

Show the step-by-step approach

How the model solution is built, in the order the database evaluates it:

  1. Read from the employees table.
  2. Fetch salary from the previous row with LAG(), ordered by hire_date. Where there is no such row the value is NULL.
  3. Combine those values into pct_diff: (salary - LAG(salary) OVER (ORDER BY hire_date)) * 100.0 / LAG(salary) OVER (ORDER BY hire_date).
  4. Sort the result by pct_diff descending.
Show the model solution
SELECT id, (salary - LAG(salary) OVER (ORDER BY hire_date)) * 100.0 / LAG(salary) OVER (ORDER BY hire_date) AS pct_diff FROM employees ORDER BY pct_diff DESC;

This query produced the expected output above. Yours does not have to match it word for word: any query that returns the same rows is correct.

What to watch for

  • LAG(). LAG() returns NULL for the first row of each partition, and any arithmetic with that NULL is NULL too. Supply a default as the third argument if you need a number.
  • Calculated columns. Dividing one integer by another truncates the result in SQLite. Multiply one side by 1.0 to get decimals.
  • ORDER BY. Without ORDER BY the row order is not guaranteed, even if it looks stable. Rows that tie on the sort columns can come back in any order.
Solve This SQL Challenge

Keep learning