1
+ -- 1075. Project Employees I
2
+ -- Easy
3
+ -- https://leetcode.com/problems/project-employees-i
4
+
5
+ /*
6
+ Table: Project
7
+ +-------------+---------+
8
+ | Column Name | Type |
9
+ +-------------+---------+
10
+ | project_id | int |
11
+ | employee_id | int |
12
+ +-------------+---------+
13
+ (project_id, employee_id) is the primary key of this table.
14
+ employee_id is a foreign key to Employee table.
15
+ Each row of this table indicates that the employee with employee_id is working on the project with project_id.
16
+
17
+ Table: Employee
18
+ +------------------+---------+
19
+ | Column Name | Type |
20
+ +------------------+---------+
21
+ | employee_id | int |
22
+ | name | varchar |
23
+ | experience_years | int |
24
+ +------------------+---------+
25
+ employee_id is the primary key of this table. It's guaranteed that experience_years is not NULL.
26
+ Each row of this table contains information about one employee.
27
+
28
+ Write an SQL query that reports the average experience years of all the employees for each project, rounded to 2 digits.
29
+ Return the result table in any order.
30
+ The query result format is in the following example.
31
+
32
+ Example 1:
33
+ Input:
34
+ Project table:
35
+ +-------------+-------------+
36
+ | project_id | employee_id |
37
+ +-------------+-------------+
38
+ | 1 | 1 |
39
+ | 1 | 2 |
40
+ | 1 | 3 |
41
+ | 2 | 1 |
42
+ | 2 | 4 |
43
+ +-------------+-------------+
44
+ Employee table:
45
+ +-------------+--------+------------------+
46
+ | employee_id | name | experience_years |
47
+ +-------------+--------+------------------+
48
+ | 1 | Khaled | 3 |
49
+ | 2 | Ali | 2 |
50
+ | 3 | John | 1 |
51
+ | 4 | Doe | 2 |
52
+ +-------------+--------+------------------+
53
+ Output:
54
+ +-------------+---------------+
55
+ | project_id | average_years |
56
+ +-------------+---------------+
57
+ | 1 | 2.00 |
58
+ | 2 | 2.50 |
59
+ +-------------+---------------+
60
+ Explanation: The average experience years for the first project is (3 + 2 + 1) / 3 = 2.00 and for the second project is (3 + 2) / 2 = 2.50
61
+ */
62
+
63
+ SELECT
64
+ p .project_id ,
65
+ ROUND(AVG (e .experience_years ), 2 ) as average_years
66
+ FROM
67
+ Project p
68
+ JOIN
69
+ Employee e ON p .employee_id = e .employee_id
70
+ GROUP BY
71
+ p .project_id ;
0 commit comments