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:

  1. ๐Ÿ“Š Must have at least 3 reviews total
  2. ๐Ÿ“ˆ Last 3 reviews must show strictly increasing ratings (each better than previous)
  3. ๐ŸŽฏ 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
๐Ÿƒโ€โ™‚๏ธ Employee Performance JourneyAlice (Improving!)2Jan 20233Feb 20235Mar 2023Score: 5-2 = 3 โœ“Bob (Declining)4Jan 20234Feb 20233Mar 2023Not improving โœ—Carol (Improving!)1Jan 20233Feb 20234Mar 2023Score: 4-1 = 3 โœ“๐Ÿ† Final Results (Sorted by Score DESC, Name ASC)1. Alice - Score: 3 (2โ†’3โ†’5)2. Carol - Score: 3 (1โ†’3โ†’4)Bob excluded: No improvement pattern
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.
Asked in
Meta 35 Amazon 28 Google 22 Microsoft 18
28.5K Views
Medium Frequency
~25 min Avg. Time
847 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