🎯 Problem: Project Employees II
📖 The Real Problem
You have two tables:
Project table:
- project_id (int)
- employee_id (int)
Employee table:
- employee_id (int)
- name (varchar)
- experience_years (int)
Your task: For each project, calculate the AVERAGE experience years of all employees on that project.
The Challenge:
- Need to JOIN two tables
- Group results by project
- Calculate average with proper rounding (2 decimal places)
- Handle projects with different team sizes
Why this problem exists:
- Real-world analytics: team composition analysis
- Tests SQL JOIN and aggregation skills
- Requires understanding of GROUP BY and AVG()
💡 Why This Matters
Real-world applications:
- Team Analytics - Analyze team experience levels
- Resource Planning - Balance experience across projects
- Budget Planning - Senior teams cost more
- Project Bidding - Estimate based on team expertise
Skills you'll develop:
- ✅ SQL JOIN operations
- ✅ GROUP BY clause
- ✅ AVG() aggregate function
- ✅ ROUND() for decimal precision
- ✅ Data aggregation across tables
📋 Contributor Tasks
Step 1: Understand the Data
- Examine Project table (project_id, employee_id)
- Examine Employee table (employee_id, name, experience_years)
- Understand relationship: Project references Employee via employee_id
- Goal: Calculate average experience per project
Step 2: Plan Your Approach
- JOIN Project and Employee tables on employee_id
- GROUP BY project_id
- Calculate AVG(experience_years) for each group
- ROUND to 2 decimal places
Step 3: Implement the Solution
- Write SELECT with project_id and AVG calculation
- JOIN Project and Employee
- GROUP BY project_id
- Use ROUND(AVG(...), 2) for proper formatting
Step 4: Test Your Solution
- Test with single employee per project
- Test with multiple employees
- Test with varying experience levels
- Verify rounding works correctly
✅ Expected Outcome
SQL Query Structure:
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
Expected Behavior:
- ✅ Returns one row per project
- ✅ Includes project_id and average_years
- ✅ Average rounded to 2 decimal places
- ✅ All projects included (even with 1 employee)
- ✅ Correct calculation (sum / count)
Example Test Case:
-- Input:
-- Project table:
-- +-------------+-------------+
-- | project_id | employee_id |
-- +-------------+-------------+
-- | 1 | 1 |
-- | 1 | 2 |
-- | 1 | 3 |
-- | 2 | 1 |
-- | 2 | 4 |
-- +-------------+-------------+
-- Employee table:
-- +-------------+--------+------------------+
-- | employee_id | name | experience_years |
-- +-------------+--------+------------------+
-- | 1 | Khaled | 3 |
-- | 2 | Ali | 2 |
-- | 3 | John | 3 |
-- | 4 | Doe | 4 |
-- +-------------+--------+------------------+
-- Output:
-- +-------------+---------------+
-- | project_id | average_years |
-- +-------------+---------------+
-- | 1 | 2.67 | -- (3+2+3)/3 = 8/3 = 2.666... → 2.67
-- | 2 | 3.50 | -- (3+4)/2 = 7/2 = 3.5 → 3.50
-- +-------------+---------------+
📚 Additional Context & References
Understanding the Problem
Key Operations:
- JOIN - Combine Project and Employee data
- GROUP BY - Aggregate by project
- AVG() - Calculate mean experience
- ROUND() - Format to 2 decimals
SQL Order of Execution:
FROM → JOIN → WHERE → GROUP BY → SELECT → ORDER BY
Solution Approach
Standard SQL Solution:
SELECT
p.project_id,
ROUND(AVG(e.experience_years), 2) as average_years
FROM Project p
INNER JOIN Employee e
ON p.employee_id = e.employee_id
GROUP BY p.project_id
ORDER BY p.project_id
Hints (Use Only If Stuck!)
💡 Hint 1
You need to combine data from both tables. What SQL operation does that?
💡 Hint 2
To calculate per-project statistics, you need to group the results by project_id.
💡 Hint 3
Use ROUND(AVG(column), 2) to get the average rounded to 2 decimal places.
Complexity Analysis
Time Complexity: O(n log n)
- JOIN: O(n log n) with indexes
- GROUP BY: O(n log n) for sorting
- AVG calculation: O(n)
Space Complexity: O(n)
- Temporary storage for JOIN results
- Group buckets for aggregation
SQL Functions Used
AVG():
AVG(column) -- Calculates arithmetic mean
-- Example: AVG(3, 2, 3) = 8/3 = 2.666...
ROUND():
ROUND(value, decimals) -- Rounds to specified decimal places
-- Example: ROUND(2.666..., 2) = 2.67
GROUP BY:
GROUP BY column -- Groups rows by column value
-- Used with aggregates: COUNT, SUM, AVG, MIN, MAX
Edge Cases to Consider
- Empty tables: Return empty result
- Single employee: Average equals that employee's experience
- All same experience: Average equals that value
- NULL values: AVG ignores NULLs (standard SQL behavior)
- Large numbers: Ensure no overflow in calculation
Related Problems
Once you solve this, try:
- Project Employees I - Basic JOIN
- Project Employees III - Find MAX with ties
- Employee Bonus - Similar JOIN pattern
- Customer Placing the Largest Order - Aggregation
Helpful Resources
📝 Notes
- Round to exactly 2 decimal places
- Use standard SQL rounding rules
- Each project appears exactly once in output
- experience_years is guaranteed to be non-NULL
- Output columns: project_id, average_years
Ready to contribute?
- Fork the repository
- Create your SQL solution file
- Test with provided examples
- Submit a pull request!
File Location: exercises/1000_programs/medium/1076_project_employees_ii.sql
🚀 Happy coding!
🎯 Problem: Project Employees II
📖 The Real Problem
You have two tables:
Project table:
Employee table:
Your task: For each project, calculate the AVERAGE experience years of all employees on that project.
The Challenge:
Why this problem exists:
💡 Why This Matters
Real-world applications:
Skills you'll develop:
📋 Contributor Tasks
Step 1: Understand the Data
Step 2: Plan Your Approach
Step 3: Implement the Solution
Step 4: Test Your Solution
✅ Expected Outcome
SQL Query Structure:
Expected Behavior:
Example Test Case:
📚 Additional Context & References
Understanding the Problem
Key Operations:
SQL Order of Execution:
Solution Approach
Standard SQL Solution:
Hints (Use Only If Stuck!)
💡 Hint 1
You need to combine data from both tables. What SQL operation does that?💡 Hint 2
To calculate per-project statistics, you need to group the results by project_id.💡 Hint 3
Use ROUND(AVG(column), 2) to get the average rounded to 2 decimal places.Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
SQL Functions Used
AVG():
ROUND():
GROUP BY:
Edge Cases to Consider
Related Problems
Once you solve this, try:
Helpful Resources
📝 Notes
Ready to contribute?
File Location:
exercises/1000_programs/medium/1076_project_employees_ii.sql🚀 Happy coding!