Skip to content

1075. Project Employees I

Problem Statement

Table: Project

Column NameType
project_idint
employee_idint

(project_id, employee_id) is the primary key of this table.

employee_id is a foreign key to Employee table.

Each row of this table indicates that the employee with employee_id is working on the project with project_id.

Table: Employee

Column NameType
employee_idint
namevarchar
experience_yearsint

employee_id is the primary key of this table.

It's guaranteed that experience_years is not NULL.

Each row of this table contains information about one employee.

Instructions

  • Write an SQL query that reports the average experience years of all the employees for each project, rounded to 2 digits.
  • Return the result table in any order.
  • The query result format is in the following example.

Example

Input:

Project table

project_idemployee_id
11
12
13
21
24

Employee table

employee_idnameexperience_years
1Khaled3
2Ali2
3John1
4Doe2

Output:

project_idaverage_years
12.00
22.50

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

Submissions

sql
SELECT
    p.project_id,
    ROUND(AVG(e.experience_years), 2) AS average_years
FROM
    Project p
JOIN
    Employee e ON p.employee_id = e.employee_id
GROUP BY
    p.project_id;

Explanations

PostgreSQL
Submitted by @noeyislearning
  • SELECT p.project_id: Select the project_id from the Project table.
    • ROUND(AVG(e.experience_years), 2) AS average_years: Calculate the average experience years of all employees for each project and round it to 2 digits.
  • FROM Project p: Select the Project table and alias it as p.
  • JOIN Employee e ON p.employee_id = e.employee_id: Join the Employee table with the Project table on the employee_id.
  • GROUP BY p.project_id: Group the result by project_id.