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.
The `projects` table has one row per project with columns `project_id` (INTEGER, primary key), `project_name` (VARCHAR(100)), `start_date` (DATE, the date the project started, may be NULL) and `end_date` (DATE, the date the project finished, NULL if not finished). The table has 1 to 100,000 rows.
Return the average number of days between `start_date` and `end_date` across all completed projects in `projects`. A completed project is one with both dates present. Ignore rows where either date is NULL: leave them out of both the sum and the count. Because `project_id` is the primary key, each project is counted once.
The duration is the plain difference `end_date - start_date` 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. When both dates are present, `start_date` is on or before `end_date`.
Return exactly one row with a single column `avg_completion_days`: the mean duration in days over projects with both dates present, rounded to 2 decimal places. If no project has both dates, return one row with `avg_completion_days` equal to NULL. The result is always a single row, so no ordering is needed.
Tables
projects(project_id INTEGER, project_name VARCHAR(100), start_date DATE, end_date DATE)
Hints
- Check what type PostgreSQL gives you when you subtract one DATE from another, and what type ROUND(x, 2) accepts.