1
+ 1075 . Project Employees I
2
+ Solved
3
+ Easy
4
+ Topics
5
+ Companies
6
+ SQL Schema
7
+ Pandas Schema
8
+ Table: Project
9
+
10
+ + -- -----------+---------+
11
+ | Column Name | Type |
12
+ + -- -----------+---------+
13
+ | project_id | int |
14
+ | employee_id | int |
15
+ + -- -----------+---------+
16
+ (project_id, employee_id) is the primary key of this table.
17
+ employee_id is a foreign key to Employee table.
18
+ Each row of this table indicates that the employee with employee_id is working on the project with project_id.
19
+
20
+
21
+ Table: Employee
22
+
23
+ + -- ----------------+---------+
24
+ | Column Name | Type |
25
+ + -- ----------------+---------+
26
+ | employee_id | int |
27
+ | name | varchar |
28
+ | experience_years | int |
29
+ + -- ----------------+---------+
30
+ employee_id is the primary key of this table. It' s guaranteed that experience_years is not NULL.
31
+ Each row of this table contains information about one employee.
32
+
33
+
34
+ Write an SQL query that reports the average experience years of all the employees for each project, rounded to 2 digits.
35
+
36
+ Return the result table in any order.
37
+
38
+ The query result format is in the following example.
39
+
40
+
41
+
42
+ Example 1:
43
+
44
+ Input:
45
+ Project table:
46
+ +-------------+-------------+
47
+ | project_id | employee_id |
48
+ +-------------+-------------+
49
+ | 1 | 1 |
50
+ | 1 | 2 |
51
+ | 1 | 3 |
52
+ | 2 | 1 |
53
+ | 2 | 4 |
54
+ +-------------+-------------+
55
+ Employee table:
56
+ +-------------+--------+------------------+
57
+ | employee_id | name | experience_years |
58
+ +-------------+--------+------------------+
59
+ | 1 | Khaled | 3 |
60
+ | 2 | Ali | 2 |
61
+ | 3 | John | 1 |
62
+ | 4 | Doe | 2 |
63
+ +-------------+--------+------------------+
64
+ Output:
65
+ +-------------+---------------+
66
+ | project_id | average_years |
67
+ +-------------+---------------+
68
+ | 1 | 2.00 |
69
+ | 2 | 2.50 |
70
+ +-------------+---------------+
71
+ 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
72
+
73
+ ' ' ' SQL
74
+
75
+ # Write your MySQL query statement below
76
+ select project_id , round(avg (experience_years), 2 ) as average_years
77
+ from project as p
78
+ left join employee as e
79
+ on p .employee_id = e .employee_id
80
+ group by project_id;
81
+
82
+ ' ' '
0 commit comments