diff --git a/README.md b/README.md index 437c301..78d4f5f 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,7 @@ Useful for preparing for technical interviews and improving your SQL skills. - [3497. Analyze Subscription Conversion](./leetcode/medium/3497.%20Analyze%20Subscription%20Conversion.sql) - [3521. Find Product Recommendation Pairs](./leetcode/medium/3521.%20Find%20Product%20Recommendation%20Pairs.sql) - [3564. Seasonal Sales Analysis](./leetcode/medium/3564.%20Seasonal%20Sales%20Analysis.sql) + - [3580. Find Consistently Improving Employees](./leetcode/medium/3580.%20Find%20Consistently%20Improving%20Employees.sql) - [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql) 3. [Hard](./leetcode/hard/) - [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql) diff --git a/leetcode/medium/3580. Find Consistently Improving Employees.sql b/leetcode/medium/3580. Find Consistently Improving Employees.sql new file mode 100644 index 0000000..cfc6d24 --- /dev/null +++ b/leetcode/medium/3580. Find Consistently Improving Employees.sql @@ -0,0 +1,68 @@ +/* +Question 3580. Find Consistently Improving Employees +Link: https://leetcode.com/problems/find-consistently-improving-employees/description/?envType=problem-list-v2&envId=database + +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. +*/ + +WITH review_dates AS ( + SELECT + employee_id, + MAX(review_date) AS review_date + FROM performance_reviews + GROUP BY employee_id +), + +review_ratings AS ( + SELECT + pr.employee_id, + pr.review_date, + pr.rating, + LAG(pr.rating, 1, NULL) OVER (PARTITION BY pr.employee_id ORDER BY pr.review_date) AS second_rating, + LAG(pr.rating, 2, NULL) OVER (PARTITION BY pr.employee_id ORDER BY pr.review_date) AS third_rating + FROM performance_reviews AS pr +) + +SELECT + e.employee_id, + e.name, + rr.rating - rr.third_rating AS improvement_score +FROM employees AS e +LEFT JOIN + review_dates AS rd + ON e.employee_id = rd.employee_id +INNER JOIN + review_ratings AS rr + ON + e.employee_id = rr.employee_id + AND rd.review_date = rr.review_date +WHERE rr.rating > rr.second_rating AND rr.second_rating > rr.third_rating +ORDER BY improvement_score DESC, e.name ASC