Find Consistently Improving Employees - Problem
๐ Employee Performance Tracker
Imagine you're building an HR analytics system for a company that wants to identify their rising stars - employees who consistently improve their performance over time!
You have two tables:
- employees: Contains basic employee information (id and name)
- performance_reviews: Contains performance ratings (1-5 scale, where 5 is excellent)
Your mission: Find employees who show consistent improvement in their last 3 performance reviews. Here's what qualifies:
- ๐ Must have at least 3 reviews total
- ๐ Last 3 reviews must show strictly increasing ratings (each better than previous)
- ๐ฏ Calculate an improvement score = (latest rating - earliest rating among last 3)
Return results ordered by improvement score (highest first), then by name alphabetically.
Example: If an employee's last 3 reviews were ratings [2, 3, 5], they qualify with improvement score = 5 - 2 = 3
Input & Output
example_1.sql โ Basic Example
$
Input:
employees: [(1, 'Alice'), (2, 'Bob'), (3, 'Charlie')]
performance_reviews: [(1, 1, '2023-01-01', 2), (2, 1, '2023-02-01', 3), (3, 1, '2023-03-01', 5), (4, 2, '2023-01-01', 4), (5, 2, '2023-02-01', 4), (6, 2, '2023-03-01', 3)]
โบ
Output:
[{employee_id: 1, name: 'Alice', improvement_score: 3}]
๐ก Note:
Alice has reviews [2,3,5] which show consistent improvement (2<3<5). Improvement score = 5-2 = 3. Bob has [4,4,3] which is not improving, so he's excluded.
example_2.sql โ Multiple Qualifying Employees
$
Input:
employees: [(1, 'David'), (2, 'Emma'), (3, 'Frank')]
performance_reviews: [(1, 1, '2023-01-01', 1), (2, 1, '2023-02-01', 3), (3, 1, '2023-03-01', 4), (4, 2, '2023-01-01', 2), (5, 2, '2023-02-01', 4), (6, 2, '2023-03-01', 5)]
โบ
Output:
[{employee_id: 2, name: 'Emma', improvement_score: 3}, {employee_id: 1, name: 'David', improvement_score: 3}]
๐ก Note:
Both David [1,3,4] and Emma [2,4,5] show improvement. Both have score 3, so ordered by name: Emma first, then David.
example_3.sql โ Edge Case: Insufficient Reviews
$
Input:
employees: [(1, 'Grace'), (2, 'Henry')]
performance_reviews: [(1, 1, '2023-01-01', 3), (2, 1, '2023-02-01', 4), (3, 2, '2023-01-01', 1)]
โบ
Output:
[]
๐ก Note:
Grace has only 2 reviews [3,4], Henry has only 1 review [1]. Neither has the required minimum 3 reviews, so no employees qualify.
Constraints
- 1 โค employee_id โค 104
- 1 โค review_id โค 105
- 1 โค rating โค 5
- review_date is a valid date string
- Each employee must have at least 3 reviews to be considered
- Employee names are unique and non-empty strings
Visualization
Tap to expand
Understanding the Visualization
1
Group by Employee
Separate all performance reviews by employee (like organizing race results by runner)
2
Sort by Date
Order each employee's reviews from most recent to oldest
3
Take Last 3
Focus only on the most recent 3 performance reviews for each employee
4
Check Improvement
Verify that ratings form a strictly increasing sequence: oldest < middle < newest
5
Calculate Score
Improvement score = newest rating - oldest rating among the last 3 reviews
Key Takeaway
๐ฏ Key Insight: Use window functions to partition data by employee and efficiently compare consecutive ratings using LAG() - this avoids expensive correlated subqueries and processes data in a single pass.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code