Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Window Functions for Salary Trends
00:00
5 left

Window Functions for Salary Trends

MediumSQL · PostgreSQL

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
Interviewer

Your question is Window Functions for Salary Trends. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.