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 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
Employee Salary History Analysis ProcessStep 1: Raw DataEmp1: $50k, $60k, $75kEmp2: $55k, $65kEmp3: $70k, $80k, $90kMultiple records per employeeStep 2: Group by EmpEmployee 1 Group$50k, $60k, $75kEmployee 2 Group$55k, $65kPARTITION BY emp_idStep 3: Rank Salaries$75k(1), $60k(2), $50k(3)Emp 1 Rankings$65k(1), $55k(2)Emp 2 RankingsORDER BY salary DESCStep 4: LatestEmp1: $75kEmp2: $65kEmp3: $90kWHERE rank = 1SQL Window Function ImplementationROW_NUMBER() OVER (PARTITION BY emp_id ORDER BY CAST(salary AS SIGNED) DESC)This assigns rank 1 to the highest salary within each employee's records
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.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
23.4K Views
Medium Frequency
~12 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen