Skip to content

Commit dcd730b

Browse files
authored
Merge pull request #87 from remy727/1075-project-employees-i
1075. Project Employees I
2 parents 9374271 + 3a1c1a1 commit dcd730b

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@
384384
| 595 | Big Countries | [MySQL](./database/0595-big-countries.sql) | Easy |
385385
| 620 | Not Boring Movies | [MySQL](./database/0620-not-boring-movies.sql) | Easy |
386386
| 1068 | Product Sales Analysis I | [Ruby](./database/1068-product-sales-analysis-i.sql) | Easy |
387+
| 1075 | Project Employees I | [Ruby](./database/1075-project-employees-i.sql) | Easy |
387388
| 1148 | Article Views I | [MySQL](./database/1148-article-views-i.sql) | Easy |
388389
| 1251 | Average Selling Price | [MySQL](./database/1251-average-selling-price.sql) | Easy |
389390
| 1280 | Students and Examinations | [MySQL](./database/1280-students-and-examinations.sql) | Easy |

database/1075-project-employees-i.sql

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)