Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Date-Based Employee Lookup

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

Your question is SQL Date-Based Employee Lookup. Start with the requirements and the two 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

Abzooba maintains employee salary history with an effective date for each salary change. Write a PostgreSQL query that reports each employee's latest salary effective on the current date and the salary immediately before it.

Requirements

  1. Consider only salary records where effective_from <= CURRENT_DATE.
  2. Return every employee, including employees without an effective salary record.
  3. Use a window function to identify the latest salary and its predecessor for each employee.
  4. Return the employee ID, name, current salary, previous salary, and current salary effective date, ordered by employee ID.

Schema

employees
ColumnTypeDescription
employee_idPKINTEGERUnique employee identifier
employee_nameVARCHAR(100)Employee's full name
departmentVARCHAR(80)Abzooba department
salary_history
ColumnTypeDescription
salary_history_idPKINTEGERUnique salary history identifier
employee_idINTEGERReferences employees.employee_id
salaryNUMERIC(12,2)Salary amount
effective_fromDATEDate the salary became effective
effective_toDATEDate the salary ended, if known
Tablesemployeessalary_history
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results