Average task completion hours per project and completion month

Quick Overview

Write a SQL query that reports the average task completion time in hours for each project and completion month, counting only tasks with both a start and a completion timestamp. It tests joins, timestamp arithmetic, month bucketing, NULL filtering and rounding.

Average task completion hours per project and completion month

Company: Asana

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A project tracker stores projects and their tasks. For each project and month of completion, compute the average task completion time in hours (hours between `started_at` and `completed_at`), considering only tasks where both are non-NULL. Return `project_id`, `project_name`, `month` (completion month as `YYYY-MM`) and `avg_completion_hours` (rounded to 2 decimals). Order by `month` ascending, then `project_id` ascending. ```hint Measuring the duration Subtracting two timestamps gives an interval. Check that the way you turn that interval into hours keeps the days and the minutes, and does not keep only the hour field. ``` ### Input Table(s) `projects` | Column | Type | Meaning | |---|---|---| | project_id | INT | Project id | | project_name | VARCHAR(100), NOT NULL | Project name | Primary key: `project_id`. `tasks` | Column | Type | Meaning | |---|---|---| | task_id | INT | Task id | | project_id | INT | Project the task belongs to | | task_name | VARCHAR(100) | Task name | | started_at | TIMESTAMP | When work on the task started, NULL if it never started | | completed_at | TIMESTAMP | When the task was completed, NULL if it is not done | Primary key: `task_id`. Foreign key: `tasks.project_id` references `projects.project_id`. ### Output Contract - Columns, in this order: `project_id`, `project_name`, `month`, `avg_completion_hours`. - `month` is the month of `completed_at` as text in the form `YYYY-MM`, for example `2024-01`. - `avg_completion_hours` is the mean of the exact elapsed hours of the qualifying tasks in that project and month, rounded to 2 decimals. - One row per project and completion month that has at least one qualifying task. - Order by `month` ascending, then `project_id` ascending. Each (`month`, `project_id`) pair appears once, so the order is unique. ### Example `projects` | project_id | project_name | |---|---| | 1 | Website | | 2 | Mobile App | | 3 | Data Platform | `tasks` | task_id | project_id | task_name | started_at | completed_at | |---|---|---|---|---| | 101 | 1 | Design mockups | 2024-01-02 09:00:00 | 2024-01-03 09:00:00 | | 102 | 1 | Build landing page | 2024-01-10 08:00:00 | 2024-01-10 14:30:00 | | 103 | 1 | Fix nav bug | 2024-01-30 22:00:00 | 2024-02-01 10:00:00 | | 104 | 2 | Login screen | 2024-01-15 10:00:00 | 2024-01-15 11:20:00 | | 105 | 2 | Push notifications | 2024-01-20 09:00:00 | NULL | | 106 | 2 | Crash fix | NULL | 2024-02-05 12:00:00 | | 107 | 2 | Release build | 2024-02-10 09:00:00 | 2024-02-10 09:45:00 | | 108 | 3 | Schema review | 2024-02-12 13:00:00 | NULL | Expected result: | project_id | project_name | month | avg_completion_hours | |---|---|---|---| | 1 | Website | 2024-01 | 15.25 | | 2 | Mobile App | 2024-01 | 1.33 | | 1 | Website | 2024-02 | 36.00 | | 2 | Mobile App | 2024-02 | 0.75 | Website's January tasks took 24 and 6.5 hours, so the average is 15.25. Task 103 started in January but finished on February 1 after 36 hours, so it counts in February. Tasks 105, 106 and 108 each have a NULL timestamp and are ignored, which leaves Data Platform with no row. ### Constraints and Clarifications - There are 1 to 10,000 projects and 1 to 100,000 tasks. - Each task belongs to exactly one project, and its `project_id` always matches a row in `projects`. - `started_at` ≤ `completed_at` whenever both are non-NULL. - A task counts only when both `started_at` and `completed_at` are non-NULL. - Durations are exact: minutes and seconds count as fractions of an hour. Do not truncate each task to whole hours before averaging. Round only the final average. - The month comes from `completed_at`, never from `started_at`. - A project or month with no qualifying task gets no row. Do not output zero or NULL averages. - Two projects can share a name. Group by `project_id` so they stay separate.

Overview: Write a SQL query that reports the average task completion time in hours for each project and completion month, counting only tasks with both a start and a completion timestamp. It tests joins, timestamp arithmetic, month bucketing, NULL filtering and rounding.

Asana logo
Asana
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A project tracker stores projects and their tasks. For each project and month of completion, compute the average task completion time in hours (hours between started_at and completed_at), considering only tasks where both are non-NULL. Return project_id, project_name, month (completion month as YYYY-MM) and avg_completion_hours (rounded to 2 decimals). Order by month ascending, then project_id ascending.

Input Table(s)

projects

ColumnTypeMeaning
project_idINTProject id
project_nameVARCHAR(100), NOT NULLProject name

Primary key: project_id.

tasks

ColumnTypeMeaning
task_idINTTask id
project_idINTProject the task belongs to
task_nameVARCHAR(100)Task name
started_atTIMESTAMPWhen work on the task started, NULL if it never started
completed_atTIMESTAMPWhen the task was completed, NULL if it is not done

Primary key: task_id. Foreign key: tasks.project_id references projects.project_id.

Output Contract

  • Columns, in this order: project_id , project_name , month , avg_completion_hours .
  • month is the month of completed_at as text in the form YYYY-MM , for example 2024-01 .
  • avg_completion_hours is the mean of the exact elapsed hours of the qualifying tasks in that project and month, rounded to 2 decimals.
  • One row per project and completion month that has at least one qualifying task.
  • Order by month ascending, then project_id ascending. Each ( month , project_id ) pair appears once, so the order is unique.

Example

projects

project_idproject_name
1Website
2Mobile App
3Data Platform

tasks

task_idproject_idtask_namestarted_atcompleted_at
1011Design mockups2024-01-02 09:00:002024-01-03 09:00:00
1021Build landing page2024-01-10 08:00:002024-01-10 14:30:00
1031Fix nav bug2024-01-30 22:00:002024-02-01 10:00:00
1042Login screen2024-01-15 10:00:002024-01-15 11:20:00
1052Push notifications2024-01-20 09:00:00NULL
1062Crash fixNULL2024-02-05 12:00:00
1072Release build2024-02-10 09:00:002024-02-10 09:45:00
1083Schema review2024-02-12 13:00:00NULL

Expected result:

project_idproject_namemonthavg_completion_hours
1Website2024-0115.25
2Mobile App2024-011.33
1Website2024-0236.00
2Mobile App2024-020.75

Website's January tasks took 24 and 6.5 hours, so the average is 15.25. Task 103 started in January but finished on February 1 after 36 hours, so it counts in February. Tasks 105, 106 and 108 each have a NULL timestamp and are ignored, which leaves Data Platform with no row.

Constraints and Clarifications

  • There are 1 to 10,000 projects and 1 to 100,000 tasks.
  • Each task belongs to exactly one project, and its project_id always matches a row in projects .
  • started_at ≤ completed_at whenever both are non-NULL.
  • A task counts only when both started_at and completed_at are non-NULL.
  • Durations are exact: minutes and seconds count as fractions of an hour. Do not truncate each task to whole hours before averaging. Round only the final average.
  • The month comes from completed_at , never from started_at .
  • A project or month with no qualifying task gets no row. Do not output zero or NULL averages.
  • Two projects can share a name. Group by project_id so they stay separate.