Calculate the average project completion time in days, ignoring rows with a missing date

Quick Overview

Write a SQL query that returns the average number of days between start and end dates across completed projects, skipping rows with a missing date and rounding to two decimals. It tests date arithmetic, NULL handling in aggregates and numeric rounding in PostgreSQL.

Calculate the average project completion time in days, ignoring rows with a missing date

Company: Target

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Return the average number of days between `start_date` and `end_date` across all completed projects in `Projects`. Ignore rows where either date is `NULL`, and return a single column `avg_completion_days`, rounded to 2 decimals. ```hint Date arithmetic Check what type PostgreSQL gives you when you subtract one `DATE` from another, and what type `ROUND(x, 2)` accepts. ``` ### Input Table(s) `Projects` | Column | Type | Meaning | |---|---|---| | project_id | INT | Project identifier | | project_name | VARCHAR(100) | Project name | | start_date | DATE | Date the project started, may be NULL | | end_date | DATE | Date the project finished, NULL if not finished | Primary key: `project_id`. ### Output Contract Exactly one row with one column: | Column | Definition | |---|---| | avg_completion_days | Mean of `end_date - start_date` in days over projects with both dates present, rounded to 2 decimal places | The result is always a single row, so no ordering is needed. ### Example `Projects` | project_id | project_name | start_date | end_date | |---|---|---|---| | 1 | Alpha | 2024-01-01 | 2024-01-08 | | 2 | Beta | 2024-02-01 | NULL | | 3 | Gamma | NULL | 2024-03-10 | | 4 | Delta | 2024-04-01 | 2024-04-15 | Expected result: | avg_completion_days | |---| | 10.50 | Only projects 1 and 4 have both dates. Their durations are 7 and 14 days, and the average is 10.50. ### Constraints and Clarifications - `Projects` has 1 to 100,000 rows. - `project_id` is the primary key, so each project is counted once. - `start_date` is on or before `end_date` when both are present. - A completed project is one with both dates present. Rows with either date `NULL` are left out of both the sum and the count. - The duration is the plain difference in days: a project that starts and ends on the same day has a duration of 0, and 2024-01-01 to 2024-01-08 is 7 days. - If no project has both dates, the result is one row with `avg_completion_days` equal to `NULL`.

Overview: Write a SQL query that returns the average number of days between start and end dates across completed projects, skipping rows with a missing date and rounding to two decimals. It tests date arithmetic, NULL handling in aggregates and numeric rounding in PostgreSQL.

Target logo
Target
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Return the average number of days between start_date and end_date across all completed projects in Projects. Ignore rows where either date is NULL, and return a single column avg_completion_days, rounded to 2 decimals.

Input Table(s)

Projects

ColumnTypeMeaning
project_idINTProject identifier
project_nameVARCHAR(100)Project name
start_dateDATEDate the project started, may be NULL
end_dateDATEDate the project finished, NULL if not finished

Primary key: project_id.

Output Contract

Exactly one row with one column:

ColumnDefinition
avg_completion_daysMean of end_date - start_date in days over projects with both dates present, rounded to 2 decimal places

The result is always a single row, so no ordering is needed.

Example

Projects

project_idproject_namestart_dateend_date
1Alpha2024-01-012024-01-08
2Beta2024-02-01NULL
3GammaNULL2024-03-10
4Delta2024-04-012024-04-15

Expected result:

avg_completion_days
10.50

Only projects 1 and 4 have both dates. Their durations are 7 and 14 days, and the average is 10.50.

Constraints and Clarifications

  • Projects has 1 to 100,000 rows.
  • project_id is the primary key, so each project is counted once.
  • start_date is on or before end_date when both are present.
  • A completed project is one with both dates present. Rows with either date NULL are left out of both the sum and the count.
  • The duration is the plain difference in days: a project that starts and ends on the same day has a duration of 0, and 2024-01-01 to 2024-01-08 is 7 days.
  • If no project has both dates, the result is one row with avg_completion_days equal to NULL .