Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions for Salary Trends

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Window Functions for Salary Trends. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Paylocity wants a quarterly compensation report for workforce analytics. Write a PostgreSQL query that ranks employees within their department for each quarter and measures each employee's change from the previous recorded quarter.

Requirements

  1. Join employees to departments and quarterly salary history.
  2. Derive the quarter from effective_date.
  3. Use RANK() to rank salaries within each department and quarter, with higher salaries ranked first.
  4. Use LAG() to retrieve each employee's prior-quarter salary and calculate the absolute salary change. The first recorded quarter for an employee should have a NULL change.
  5. Return results ordered by department, quarter, and employee ID.

Assume one salary record per employee per quarter. Compare each employee with their previous recorded salary quarter.

Schema

departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Department display name
employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(120)Employee full name
department_idINTReferences departments.department_id
job_titleVARCHAR(120)Employee's current job title
salary_history
ColumnTypeDescription
salary_history_idPKINTUnique salary history record
employee_idINTReferences employees.employee_id
effective_dateDATESalary effective date
salary_amountNUMERIC(12,2)Annual salary amount
Tablesdepartmentsemployeessalary_history
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results