diff --git a/src/main/kotlin/g3501_3600/s3580_find_consistently_improving_employees/readme.md b/src/main/kotlin/g3501_3600/s3580_find_consistently_improving_employees/readme.md new file mode 100644 index 00000000..2bc35eb8 --- /dev/null +++ b/src/main/kotlin/g3501_3600/s3580_find_consistently_improving_employees/readme.md @@ -0,0 +1,112 @@ +3580\. Find Consistently Improving Employees + +Medium + +Table: `employees` + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | employee_id | int | + | name | varchar | + +-------------+---------+ + employee_id is the unique identifier for this table. + Each row contains information about an employee. + +Table: `performance_reviews` + + +-------------+------+ + | Column Name | Type | + +-------------+------+ + | review_id | int | + | employee_id | int | + | review_date | date | + | rating | int | + +-------------+------+ + review_id is the unique identifier for this table. + Each row represents a performance review for an employee. + The rating is on a scale of 1-5 where 5 is excellent and 1 is poor. + +Write a solution to find employees who have consistently improved their performance over **their last three reviews**. + +* An employee must have **at least** `3` **review** to be considered +* The employee's **last** `3` **reviews** must show **strictly increasing ratings** (each review better than the previous) +* Use the most recent `3` reviews based on `review_date` for each employee +* Calculate the **improvement score** as the difference between the latest rating and the earliest rating among the last `3` reviews + +Return _the result table ordered by **improvement score** in **descending** order, then by **name** in **ascending** order_. + +The result format is in the following example. + +**Example:** + +**Input:** + +employees table: + + +-------------+----------------+ + | employee_id | name | + +-------------+----------------+ + | 1 | Alice Johnson | + | 2 | Bob Smith | + | 3 | Carol Davis | + | 4 | David Wilson | + | 5 | Emma Brown | + +-------------+----------------+ + +performance\_reviews table: + + +-----------+-------------+-------------+--------+ + | review_id | employee_id | review_date | rating | + +-----------+-------------+-------------+--------+ + | 1 | 1 | 2023-01-15 | 2 | + | 2 | 1 | 2023-04-15 | 3 | + | 3 | 1 | 2023-07-15 | 4 | + | 4 | 1 | 2023-10-15 | 5 | + | 5 | 2 | 2023-02-01 | 3 | + | 6 | 2 | 2023-05-01 | 2 | + | 7 | 2 | 2023-08-01 | 4 | + | 8 | 2 | 2023-11-01 | 5 | + | 9 | 3 | 2023-03-10 | 1 | + | 10 | 3 | 2023-06-10 | 2 | + | 11 | 3 | 2023-09-10 | 3 | + | 12 | 3 | 2023-12-10 | 4 | + | 13 | 4 | 2023-01-20 | 4 | + | 14 | 4 | 2023-04-20 | 4 | + | 15 | 4 | 2023-07-20 | 4 | + | 16 | 5 | 2023-02-15 | 3 | + | 17 | 5 | 2023-05-15 | 2 | + +-----------+-------------+-------------+--------+ + +**Output:** + + +-------------+----------------+-------------------+ + | employee_id | name | improvement_score | + +-------------+----------------+-------------------+ + | 2 | Bob Smith | 3 | + | 1 | Alice Johnson | 2 | + | 3 | Carol Davis | 2 | + +-------------+----------------+-------------------+ + +**Explanation:** + +* **Alice Johnson (employee\_id = 1):** + * Has 4 reviews with ratings: 2, 3, 4, 5 + * Last 3 reviews (by date): 2023-04-15 (3), 2023-07-15 (4), 2023-10-15 (5) + * Ratings are strictly increasing: 3 → 4 → 5 + * Improvement score: 5 - 3 = 2 +* **Carol Davis (employee\_id = 3):** + * Has 4 reviews with ratings: 1, 2, 3, 4 + * Last 3 reviews (by date): 2023-06-10 (2), 2023-09-10 (3), 2023-12-10 (4) + * Ratings are strictly increasing: 2 → 3 → 4 + * Improvement score: 4 - 2 = 2 +* **Bob Smith (employee\_id = 2):** + * Has 4 reviews with ratings: 3, 2, 4, 5 + * Last 3 reviews (by date): 2023-05-01 (2), 2023-08-01 (4), 2023-11-01 (5) + * Ratings are strictly increasing: 2 → 4 → 5 + * Improvement score: 5 - 2 = 3 +* **Employees not included:** + * David Wilson (employee\_id = 4): Last 3 reviews are all 4 (no improvement) + * Emma Brown (employee\_id = 5): Only has 2 reviews (needs at least 3) + +The output table is ordered by improvement\_score in descending order, then by name in ascending order. \ No newline at end of file diff --git a/src/main/kotlin/g3501_3600/s3580_find_consistently_improving_employees/script.sql b/src/main/kotlin/g3501_3600/s3580_find_consistently_improving_employees/script.sql new file mode 100644 index 00000000..8596bd48 --- /dev/null +++ b/src/main/kotlin/g3501_3600/s3580_find_consistently_improving_employees/script.sql @@ -0,0 +1,35 @@ +# Write your MySQL query statement below +# #Medium #Database #2025_06_11_Time_449_ms_(91.67%)_Space_0.0_MB_(100.00%) +WITH Ranked AS ( + SELECT + e.employee_id, + e.name, + pr.review_date, + pr.rating, + RANK() OVER ( + PARTITION BY e.employee_id + ORDER BY pr.review_date DESC + ) AS rnk, + LAG(pr.rating) OVER ( + PARTITION BY e.employee_id + ORDER BY pr.review_date DESC + ) AS lag_rating + FROM employees e + LEFT JOIN performance_reviews pr + ON e.employee_id = pr.employee_id +) +SELECT + employee_id, + name, + MAX(rating) - MIN(rating) AS improvement_score +FROM Ranked +WHERE rnk <= 3 +GROUP BY + employee_id, + name +HAVING + COUNT(*) = 3 + AND SUM(CASE WHEN lag_rating > rating THEN 1 ELSE 0 END) = 2 +ORDER BY + improvement_score DESC, + name ASC; diff --git a/src/test/kotlin/g3501_3600/s3580_find_consistently_improving_employees/MysqlTest.kt b/src/test/kotlin/g3501_3600/s3580_find_consistently_improving_employees/MysqlTest.kt new file mode 100644 index 00000000..a6f47094 --- /dev/null +++ b/src/test/kotlin/g3501_3600/s3580_find_consistently_improving_employees/MysqlTest.kt @@ -0,0 +1,96 @@ +package g3501_3600.s3580_find_consistently_improving_employees + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test +import org.zapodot.junit.db.annotations.EmbeddedDatabase +import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest +import org.zapodot.junit.db.common.CompatibilityMode +import java.io.BufferedReader +import java.io.FileNotFoundException +import java.io.FileReader +import java.sql.SQLException +import java.util.stream.Collectors +import javax.sql.DataSource + +@EmbeddedDatabaseTest( + compatibilityMode = CompatibilityMode.MySQL, + initialSqls = [ + ( + "CREATE TABLE employees(employee_id INTEGER, name VARCHAR(255)); " + + "INSERT INTO employees (employee_id, name) VALUES" + + " (1, 'Alice Johnson')," + + " (2, 'Bob Smith')," + + " (3, 'Carol Davis')," + + " (4, 'David Wilson')," + + " (5, 'Emma Brown');" + + "CREATE TABLE performance_reviews(review_id INTEGER, employee_id INTEGER" + + ", review_date DATE, rating INTEGER); " + + "INSERT INTO performance_reviews (review_id, employee_id, review_date, rating) VALUES" + + " (1, 1, '2023-01-15', 2)," + + " (2, 1, '2023-04-15', 3)," + + " (3, 1, '2023-07-15', 4)," + + " (4, 1, '2023-10-15', 5)," + + " (5, 2, '2023-02-01', 3)," + + " (6, 2, '2023-05-01', 2)," + + " (7, 2, '2023-08-01', 4)," + + " (8, 2, '2023-11-01', 5)," + + " (9, 3, '2023-03-10', 1)," + + " (10, 3, '2023-06-10', 2)," + + " (11, 3, '2023-09-10', 3)," + + " (12, 3, '2023-12-10', 4)," + + " (13, 4, '2023-01-20', 4)," + + " (14, 4, '2023-04-20', 4)," + + " (15, 4, '2023-07-20', 4)," + + " (16, 5, '2023-02-15', 3)," + + " (17, 5, '2023-05-15', 2);" + ), + ], +) +internal class MysqlTest { + @Test + @Throws(SQLException::class, FileNotFoundException::class) + fun testScript(@EmbeddedDatabase dataSource: DataSource) { + dataSource.connection.use { connection -> + connection.createStatement().use { statement -> + statement.executeQuery( + BufferedReader( + FileReader( + ( + "src/main/kotlin/g3501_3600/" + + "s3580_find_consistently_improving_employees/" + + "script.sql" + ), + ), + ) + .lines() + .collect(Collectors.joining("\n")) + .replace("#.*?\\r?\\n".toRegex(), ""), + ).use { resultSet -> + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("2")) + assertThat( + resultSet.getNString(2), + equalTo("Bob Smith"), + ) + assertThat(resultSet.getNString(3), equalTo("3")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("1")) + assertThat( + resultSet.getNString(2), + equalTo("Alice Johnson"), + ) + assertThat(resultSet.getNString(3), equalTo("2")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("3")) + assertThat( + resultSet.getNString(2), + equalTo("Carol Davis"), + ) + assertThat(resultSet.getNString(3), equalTo("2")) + assertThat(resultSet.next(), equalTo(false)) + } + } + } + } +}