|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3580\. Find Consistently Improving Employees |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +Table: `employees` |
| 9 | + |
| 10 | + +-------------+---------+ |
| 11 | + | Column Name | Type | |
| 12 | + +-------------+---------+ |
| 13 | + | employee_id | int | |
| 14 | + | name | varchar | |
| 15 | + +-------------+---------+ |
| 16 | + employee_id is the unique identifier for this table. |
| 17 | + Each row contains information about an employee. |
| 18 | + |
| 19 | +Table: `performance_reviews` |
| 20 | + |
| 21 | + +-------------+------+ |
| 22 | + | Column Name | Type | |
| 23 | + +-------------+------+ |
| 24 | + | review_id | int | |
| 25 | + | employee_id | int | |
| 26 | + | review_date | date | |
| 27 | + | rating | int | |
| 28 | + +-------------+------+ |
| 29 | + review_id is the unique identifier for this table. |
| 30 | + Each row represents a performance review for an employee. |
| 31 | + The rating is on a scale of 1-5 where 5 is excellent and 1 is poor. |
| 32 | + |
| 33 | +Write a solution to find employees who have consistently improved their performance over **their last three reviews**. |
| 34 | + |
| 35 | +* An employee must have **at least** `3` **review** to be considered |
| 36 | +* The employee's **last** `3` **reviews** must show **strictly increasing ratings** (each review better than the previous) |
| 37 | +* Use the most recent `3` reviews based on `review_date` for each employee |
| 38 | +* Calculate the **improvement score** as the difference between the latest rating and the earliest rating among the last `3` reviews |
| 39 | + |
| 40 | +Return _the result table ordered by **improvement score** in **descending** order, then by **name** in **ascending** order_. |
| 41 | + |
| 42 | +The result format is in the following example. |
| 43 | + |
| 44 | +**Example:** |
| 45 | + |
| 46 | +**Input:** |
| 47 | + |
| 48 | +employees table: |
| 49 | + |
| 50 | + +-------------+----------------+ |
| 51 | + | employee_id | name | |
| 52 | + +-------------+----------------+ |
| 53 | + | 1 | Alice Johnson | |
| 54 | + | 2 | Bob Smith | |
| 55 | + | 3 | Carol Davis | |
| 56 | + | 4 | David Wilson | |
| 57 | + | 5 | Emma Brown | |
| 58 | + +-------------+----------------+ |
| 59 | + |
| 60 | +performance\_reviews table: |
| 61 | + |
| 62 | + +-----------+-------------+-------------+--------+ |
| 63 | + | review_id | employee_id | review_date | rating | |
| 64 | + +-----------+-------------+-------------+--------+ |
| 65 | + | 1 | 1 | 2023-01-15 | 2 | |
| 66 | + | 2 | 1 | 2023-04-15 | 3 | |
| 67 | + | 3 | 1 | 2023-07-15 | 4 | |
| 68 | + | 4 | 1 | 2023-10-15 | 5 | |
| 69 | + | 5 | 2 | 2023-02-01 | 3 | |
| 70 | + | 6 | 2 | 2023-05-01 | 2 | |
| 71 | + | 7 | 2 | 2023-08-01 | 4 | |
| 72 | + | 8 | 2 | 2023-11-01 | 5 | |
| 73 | + | 9 | 3 | 2023-03-10 | 1 | |
| 74 | + | 10 | 3 | 2023-06-10 | 2 | |
| 75 | + | 11 | 3 | 2023-09-10 | 3 | |
| 76 | + | 12 | 3 | 2023-12-10 | 4 | |
| 77 | + | 13 | 4 | 2023-01-20 | 4 | |
| 78 | + | 14 | 4 | 2023-04-20 | 4 | |
| 79 | + | 15 | 4 | 2023-07-20 | 4 | |
| 80 | + | 16 | 5 | 2023-02-15 | 3 | |
| 81 | + | 17 | 5 | 2023-05-15 | 2 | |
| 82 | + +-----------+-------------+-------------+--------+ |
| 83 | + |
| 84 | +**Output:** |
| 85 | + |
| 86 | + +-------------+----------------+-------------------+ |
| 87 | + | employee_id | name | improvement_score | |
| 88 | + +-------------+----------------+-------------------+ |
| 89 | + | 2 | Bob Smith | 3 | |
| 90 | + | 1 | Alice Johnson | 2 | |
| 91 | + | 3 | Carol Davis | 2 | |
| 92 | + +-------------+----------------+-------------------+ |
| 93 | + |
| 94 | +**Explanation:** |
| 95 | + |
| 96 | +* **Alice Johnson (employee\_id = 1):** |
| 97 | + * Has 4 reviews with ratings: 2, 3, 4, 5 |
| 98 | + * Last 3 reviews (by date): 2023-04-15 (3), 2023-07-15 (4), 2023-10-15 (5) |
| 99 | + * Ratings are strictly increasing: 3 → 4 → 5 |
| 100 | + * Improvement score: 5 - 3 = 2 |
| 101 | +* **Carol Davis (employee\_id = 3):** |
| 102 | + * Has 4 reviews with ratings: 1, 2, 3, 4 |
| 103 | + * Last 3 reviews (by date): 2023-06-10 (2), 2023-09-10 (3), 2023-12-10 (4) |
| 104 | + * Ratings are strictly increasing: 2 → 3 → 4 |
| 105 | + * Improvement score: 4 - 2 = 2 |
| 106 | +* **Bob Smith (employee\_id = 2):** |
| 107 | + * Has 4 reviews with ratings: 3, 2, 4, 5 |
| 108 | + * Last 3 reviews (by date): 2023-05-01 (2), 2023-08-01 (4), 2023-11-01 (5) |
| 109 | + * Ratings are strictly increasing: 2 → 4 → 5 |
| 110 | + * Improvement score: 5 - 2 = 3 |
| 111 | +* **Employees not included:** |
| 112 | + * David Wilson (employee\_id = 4): Last 3 reviews are all 4 (no improvement) |
| 113 | + * Emma Brown (employee\_id = 5): Only has 2 reviews (needs at least 3) |
| 114 | + |
| 115 | +The output table is ordered by improvement\_score in descending order, then by name in ascending order. |
| 116 | + |
| 117 | +## Solution |
| 118 | + |
| 119 | +```sql |
| 120 | +# Write your MySQL query statement below |
| 121 | +WITH Ranked AS ( |
| 122 | + SELECT |
| 123 | + e.employee_id, |
| 124 | + e.name, |
| 125 | + pr.review_date, |
| 126 | + pr.rating, |
| 127 | + RANK() OVER ( |
| 128 | + PARTITION BY e.employee_id |
| 129 | + ORDER BY pr.review_date DESC |
| 130 | + ) AS rnk, |
| 131 | + LAG(pr.rating) OVER ( |
| 132 | + PARTITION BY e.employee_id |
| 133 | + ORDER BY pr.review_date DESC |
| 134 | + ) AS lag_rating |
| 135 | + FROM employees e |
| 136 | + LEFT JOIN performance_reviews pr |
| 137 | + ON e.employee_id = pr.employee_id |
| 138 | +) |
| 139 | +SELECT |
| 140 | + employee_id, |
| 141 | + name, |
| 142 | + MAX(rating) - MIN(rating) AS improvement_score |
| 143 | +FROM Ranked |
| 144 | +WHERE rnk <= 3 |
| 145 | +GROUP BY |
| 146 | + employee_id, |
| 147 | + name |
| 148 | +HAVING |
| 149 | + COUNT(*) = 3 |
| 150 | + AND SUM(CASE WHEN lag_rating > rating THEN 1 ELSE 0 END) = 2 |
| 151 | +ORDER BY |
| 152 | + improvement_score DESC, |
| 153 | + name ASC; |
| 154 | +``` |
0 commit comments