Description
Table: Project
| Column Name | Type |
|---|---|
| project_id | int |
| employee_id | int |
- (
project_id,employee_id) is the primary key (combination of columns with unique values) of this table. employee_idis a foreign key (reference column) to Employee table.- Each row of this table indicates that the employee with
employee_idis working on the project withproject_id.
Table: Employee
| Column Name | Type |
|---|---|
| employee_id | int |
| name | varchar |
| experience_years | int |
employee_idis the primary key (column with unique values) of this table.- Each row of this table contains information about one employee.
Problem Statement
Write a solution to report the most experienced employees in each project. In case of a tie, report all employees with the maximum number of experience years.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Project table:
| project_id | employee_id |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 4 |
Employee table:
| employee_id | name | experience_years |
|---|---|---|
| 1 | Khaled | 3 |
| 2 | Ali | 2 |
| 3 | John | 3 |
| 4 | Doe | 2 |
Output:
| project_id | employee_id |
|---|---|
| 1 | 1 |
| 1 | 3 |
| 2 | 1 |
Explanation:
- Both employees with id
1and3have the most experience among the employees of the first project. For the second project, the employee with id1has the most experience.
Solution
The problem is asking for the most experienced employees in each project. This can be solved using window functions easily.
Approach 1: Using Window Functions
You can create a partition for each project using project_id column and order the records using experience_years column. This will give you the employees with maximum experience in each project. Now, in case of a tie, you want to report all employees with same experience. This means you need to use RANK() or DENSE_RANK() for getting the most experienced employee per project.
- Find employees for each project. For this, you need to join
Projecttable withEmployeetable.
1SELECT
2 p.project_id, e.employee_id, e.name, e.experience_years
3 FROM Project p
4 JOIN Employee e
5 ON p.employee_id = e.employee_id;
- Use
PARTITION BYclause to create a partition for each project and order the results for each partition usingexperience_yearscolumn. UseRANK()orDENSE_RANK()window function to get the most experienced employee per project.
1DENSE_RANK() OVER (PARTITION BY p.project_id ORDER BY e.experience_years DESC) ranking
For example, below query will give you ranking for each project_id.
1SELECT
2 p.project_id, e.employee_id, e.name, e.experience_years,
3 DENSE_RANK() OVER (PARTITION BY p.project_id ORDER BY e.experience_years DESC) ranking
4 FROM Project p
5 JOIN Employee e
6 ON p.employee_id = e.employee_id;
| project_id | employee_id | name | experience_years | ranking |
|---|---|---|---|---|
| 1 | 1 | Khaled | 3 | 1 |
| 1 | 3 | John | 3 | 1 |
| 1 | 2 | Ali | 2 | 2 |
| 2 | 1 | Khaled | 3 | 1 |
| 2 | 4 | Doe | 2 | 2 |
- Create a temporary view from above query and Filter the results to get the employees with maximum experience in each project. This can be done using
WHERE ranking = 1clause.
1SELECT project_id, employee_id
2 FROM ranked_employees
3 WHERE ranking = 1;
The final solution looks like this.
1WITH ranked_employees AS (
2 SELECT
3 p.project_id, e.employee_id, e.name, e.experience_years,
4 DENSE_RANK() OVER (PARTITION BY p.project_id ORDER BY e.experience_years DESC) ranking
5 FROM Project p
6 JOIN Employee e
7 ON p.employee_id = e.employee_id
8) SELECT project_id, employee_id
9 FROM ranked_employees
10 WHERE ranking = 1;


Comments