Find Latest Salaries - Problem
Employee Salary History Analysis
You work as a data analyst for a company that maintains employee salary records in a database. However, the database contains historical salary data - meaning multiple salary entries exist for the same employee from different years.
Given a
Goal: Extract the most recent salary record for each employee
Input: A salary table with potentially multiple records per employee
Output: One record per employee showing their current salary information
The result should include
You work as a data analyst for a company that maintains employee salary records in a database. However, the database contains historical salary data - meaning multiple salary entries exist for the same employee from different years.
Given a
Salary table with employee information and their salaries over time, your task is to find the current (latest) salary for each employee. The key insight is that salaries always increase each year, so the highest salary for each employee represents their current compensation.Goal: Extract the most recent salary record for each employee
Input: A salary table with potentially multiple records per employee
Output: One record per employee showing their current salary information
The result should include
emp_id, firstname, lastname, salary, and department_id, ordered by employee ID ascending. Input & Output
Basic Example
$
Input:
Salary table:
| emp_id | firstname | lastname | salary | department_id |
|--------|-----------|----------|--------|--------------|
| 1 | John | Smith | 50000 | IT |
| 1 | John | Smith | 60000 | IT |
| 1 | John | Smith | 75000 | IT |
| 2 | Jane | Doe | 55000 | HR |
| 2 | Jane | Doe | 65000 | HR |
โบ
Output:
| emp_id | firstname | lastname | salary | department_id |
|--------|-----------|----------|--------|--------------|
| 1 | John | Smith | 75000 | IT |
| 2 | Jane | Doe | 65000 | HR |
๐ก Note:
John has three salary records, with $75000 being the highest (most recent). Jane has two records, with $65000 being the highest. These represent their current salaries.
Single Record Per Employee
$
Input:
Salary table:
| emp_id | firstname | lastname | salary | department_id |
|--------|-----------|----------|--------|--------------|
| 10 | Alice | Johnson | 80000 | Engineering |
| 20 | Bob | Wilson | 70000 | Marketing |
| 30 | Carol | Brown | 90000 | Finance |
โบ
Output:
| emp_id | firstname | lastname | salary | department_id |
|--------|-----------|----------|--------|--------------|
| 10 | Alice | Johnson | 80000 | Engineering |
| 20 | Bob | Wilson | 70000 | Marketing |
| 30 | Carol | Brown | 90000 | Finance |
๐ก Note:
When each employee has only one salary record, that record represents their current salary, so all records are returned.
Multiple Employees with Mixed History
$
Input:
Salary table:
| emp_id | firstname | lastname | salary | department_id |
|--------|-----------|----------|--------|--------------|
| 100 | David | Lee | 45000 | Sales |
| 100 | David | Lee | 55000 | Sales |
| 200 | Emma | Clark | 85000 | IT |
| 300 | Frank | Davis | 40000 | Support |
| 300 | Frank | Davis | 48000 | Support |
| 300 | Frank | Davis | 52000 | Support |
โบ
Output:
| emp_id | firstname | lastname | salary | department_id |
|--------|-----------|----------|--------|--------------|
| 100 | David | Lee | 55000 | Sales |
| 200 | Emma | Clark | 85000 | IT |
| 300 | Frank | Davis | 52000 | Support |
๐ก Note:
David's current salary is $55000 (from 2 records), Emma has only one record at $85000, and Frank's current salary is $52000 (from 3 records). Each employee gets one row with their maximum salary.
Constraints
- 1 โค emp_id โค 104
- 1 โค firstname.length, lastname.length โค 50
- 103 โค salary โค 106 (stored as varchar but represents numeric values)
- 1 โค department_id.length โค 20
- Salary values always increase over time for each employee
Visualization
Tap to expand
Understanding the Visualization
1
Raw Data
Multiple salary records exist for each employee across different time periods
2
Group by Employee
Partition the data by employee ID to handle each employee's records separately
3
Rank by Salary
Within each employee's group, rank salaries in descending order (highest = rank 1)
4
Select Latest
Filter to keep only rank 1 records - these represent current salaries
Key Takeaway
๐ฏ Key Insight: Since salaries only increase over time, the maximum salary for each employee is guaranteed to be their most recent salary, making window functions the perfect tool for this ranking problem.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code