|
1 |
| ---1075. Project Employees I |
2 |
| --- |
3 |
| ---Table: Project |
4 |
| --- |
5 |
| ---+-------------+---------+ |
6 |
| ---| Column Name | Type | |
7 |
| ---+-------------+---------+ |
8 |
| ---| project_id | int | |
9 |
| ---| employee_id | int | |
10 |
| ---+-------------+---------+ |
11 |
| ---(project_id, employee_id) is the primary key of this table. |
12 |
| ---employee_id is a foreign key to Employee table. |
13 |
| ---Table: Employee |
14 |
| --- |
15 |
| ---+------------------+---------+ |
16 |
| ---| Column Name | Type | |
17 |
| ---+------------------+---------+ |
18 |
| ---| employee_id | int | |
19 |
| ---| name | varchar | |
20 |
| ---| experience_years | int | |
21 |
| ---+------------------+---------+ |
22 |
| ---employee_id is the primary key of this table. |
23 |
| --- |
24 |
| --- |
25 |
| ---Write an SQL query that reports the average experience years of all the employees for each project, rounded to 2 digits. |
26 |
| --- |
27 |
| ---The query result format is in the following example: |
28 |
| --- |
29 |
| ---Project table: |
30 |
| ---+-------------+-------------+ |
31 |
| ---| project_id | employee_id | |
32 |
| ---+-------------+-------------+ |
33 |
| ---| 1 | 1 | |
34 |
| ---| 1 | 2 | |
35 |
| ---| 1 | 3 | |
36 |
| ---| 2 | 1 | |
37 |
| ---| 2 | 4 | |
38 |
| ---+-------------+-------------+ |
39 |
| --- |
40 |
| ---Employee table: |
41 |
| ---+-------------+--------+------------------+ |
42 |
| ---| employee_id | name | experience_years | |
43 |
| ---+-------------+--------+------------------+ |
44 |
| ---| 1 | Khaled | 3 | |
45 |
| ---| 2 | Ali | 2 | |
46 |
| ---| 3 | John | 1 | |
47 |
| ---| 4 | Doe | 2 | |
48 |
| ---+-------------+--------+------------------+ |
49 |
| --- |
50 |
| ---Result table: |
51 |
| ---+-------------+---------------+ |
52 |
| ---| project_id | average_years | |
53 |
| ---+-------------+---------------+ |
54 |
| ---| 1 | 2.00 | |
55 |
| ---| 2 | 2.50 | |
56 |
| ---+-------------+---------------+ |
57 |
| ---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 |
58 |
| - |
59 |
| ---# Write your MySQL query statement below |
60 | 1 | select project_id, round(avg(experience_years), 2) as average_years
|
61 | 2 | from Project
|
62 | 3 | join Employee
|
|
0 commit comments