Interview Questions/SQL/Project Employees I

Project Employees I

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You have two tables: Project and Employee. Each row in Project maps an employee to a project. Each employee has an experience_years value. Write a SQL query to report the average experience years of all employees on each project, rounded to 2 decimal places. Return the result in any order.

Database Schema

Project

Column NameTypeDescription
project_idINTPart of primary key
employee_idINTPart of primary key, FK to Employee

Employee

Column NameTypeDescription
employee_idINTPrimary key
nameVARCHAREmployee name
experience_yearsINTYears of experience (≥1)

Example

Project

project_idemployee_id
11
12
13
21
24

Employee

employee_idnameexperience_years
1Khaled3
2Ali2
3John1
4Doe2

Output

project_idaverage_years
12
22.5

Explanation:

Project 1: (3+2+1)/3 = 2.00. Project 2: (3+2)/2 = 2.50.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Project

project_idemployee_id
11
12
13
21
24

Employee

employee_idnameexperience_years
1Khaled3
2Ali2
3John1
4Doe2

Output

project_idaverage_years
12
22.5