Project Duration, Employees, and Equipment Costs
Beginner Mode

Scenario

You work as a data analyst for a large construction company and have been given three tables containing project details, employee assignments, and equipment records.

Task

Write a Snowflake SQL query that:

  1. Starts from {{ ref("projects") }} as the base table and uses LEFT JOIN to bring in data from {{ ref("employees") }} and {{ ref("equipment") }} on project_id
  2. Calculates duration_days as the number of days between start_date and end_date using DATEDIFF
  3. Calculates total_employees as the count of employees per project
  4. Calculates unique_roles as the count of distinct roles per project
  5. Calculates total_equipment_cost as the sum of equipment costs per project
  6. Returns columns: project_id, project_name, start_date, end_date, duration_days, total_employees, unique_roles, total_equipment_cost
  7. Uses COALESCE to return 0 instead of NULL for projects with no employees or equipment

Schema

projects

Column Type Description
project_id Integer Unique project identifier
project_name String Name of the construction project
start_date Date Project start date
end_date Date Project end date
budget Integer Total project budget in dollars

employees

Column Type Description
employee_id Integer Unique employee identifier
first_name String Employee first name
last_name String Employee last name
role String Employee role on the project
project_id Integer Foreign key to projects

equipment

Column Type Description
equipment_id Integer Unique equipment identifier
equipment_name String Name of the equipment
project_id Integer Foreign key to projects
cost Integer Equipment cost in dollars

Example

projects:

project_id project_name start_date end_date budget
1 Riverside Tower 2023-02-01 2023-11-15 12000000
2 Harbor Bridge 2023-04-10 2023-09-30 6000000
3 Metro Tunnel 2023-01-15 2023-12-20 18000000
4 Park Pavilion 2023-06-01 2023-08-31 2000000
5 Airport Terminal 2023-03-01 2024-02-28 25000000

employees:

employee_id first_name last_name role project_id
1 Sara Chen Engineer 1
2 Mark Rivera Architect 1
3 Lisa Patel Project Manager 1
4 Tom Nguyen Engineer 2
5 Amy Garcia Foreman 2
6 Kevin Brooks Safety Officer 3
7 Rachel Kim Engineer 3
8 Dan Foster Engineer 3

equipment:

equipment_id equipment_name project_id cost
1 Crane 1 30000
2 Excavator 1 18000
3 Bulldozer 2 22000
4 Crane 3 35000
5 Loader 3 12000
6 Compactor 5 28000

Expected Output:

project_id project_name start_date end_date duration_days total_employees unique_roles total_equipment_cost
1 Riverside Tower 2023-02-01 2023-11-15 287 3 3 48000
2 Harbor Bridge 2023-04-10 2023-09-30 173 2 2 22000
3 Metro Tunnel 2023-01-15 2023-12-20 339 3 2 47000
4 Park Pavilion 2023-06-01 2023-08-31 91 0 0 0
5 Airport Terminal 2023-03-01 2024-02-28 364 0 0 28000

Note: Project 4 (Park Pavilion) has no employees and no equipment, so total_employees, unique_roles, and total_equipment_cost are all 0 (not NULL). Project 5 (Airport Terminal) has equipment but no employees. Project 3 has two Engineers, so unique_roles is 2 (not 3).

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →