# Yearly Output

> Publishing velocity for the board deck.

Canonical URL: <https://datadriven.io/problems/yearly_output>

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

How many items in `content_items` were published in each calendar year, and what was their average duration in seconds, earliest year first? Items with no publish date on file belong to no year and stay out of the annual report.

## Tables

### content_items

- content_id INTEGER
- title TEXT
- content_type TEXT
- duration_seconds INTEGER
- creator_id INTEGER
- publish_date TEXT

Sample rows (10 of 200):

| content_id | title | content_type | duration_seconds | creator_id | publish_date |
| --- | --- | --- | --- | --- | --- |
| 243 | The Ultimate Guide to AI Trends | article | NULL | 197 | 2026-02-02 |
| 286 | Top 10 Mobile Apps | podcast | 154 | 294 | 2025-03-03 |
| 329 | Understanding Machine Learning [DRAFT] | short | 201 | 391 | 2026-04-04 |
| 372 | Mastering Tech Startups | livestream | 248 | 488 | 2025-05-05 |
| 415 | Introduction to Cybersecurity | video | 295 | 585 | 2026-06-06 |
| 458 | Deep Dive: Cloud Computing | article | NULL | 682 | 2025-07-07 |
| 501 | Behind the Scenes of Product Management | podcast | 389 | 779 | 2026-08-08 |
| 544 | Breaking Down DevOps | short | 436 | 876 | 2025-09-09 |
| 587 | Exploring Web Development | livestream | 483 | NULL | 2026-01-10 |
| 630 | How to Data Science | video | 530 | 1070 | 2025-11-11 |

## Expected output

First 5 of 5 rows:

| year | items_published | avg_duration |
| --- | --- | --- |
| 2022 | 20 | NULL |
| 2023 | 20 | 2386.5 |
| 2024 | 20 | 2433.5 |
| 2025 | 67 | 2442.9824561403507 |
| 2026 | 61 | 2492.9411764705883 |

## Worked solution and explanation

### A year bucket hiding in a date column

This is a group-by on a derived key dressed up as an editorial report. `publish_date` holds full calendar dates, so grouping on the raw column hands the director one row per day instead of one per year. Everyone remembers `COUNT(*)`. What separates candidates is keying the grouping on the extracted year and guarding both kinds of missing data. Leave the undated rows in and a phantom NULL year joins the report. Divide a sum by `COUNT(*)` instead of using `AVG` and every year with missing durations looks shorter than it really is.

### Walk it through

#### Step 1: Drop undated rows first

`WHERE publish_date IS NOT NULL` removes the 12 undated rows in the sample before anything is grouped. A NULL key forms its own group, so without this filter the report gains a year with no label.

#### Step 2: Group on the extracted year

`STRFTIME('%Y', publish_date)` turns '2025-03-03' into '2025'. The `GROUP BY` must repeat that expression, not `publish_date`, or the grain of the groups stops matching the year you show.

#### Step 3: Let `AVG` skip missing durations

`AVG(duration_seconds)` averages only the durations that exist. A year where every duration is missing, like the oldest year in the preview, comes back NULL rather than a misleading 0.

#### Step 4: Sort the timeline

`ORDER BY year` makes the report read as a timeline, earliest first. It sorts five rows after the aggregate, so it costs nothing.

### The solution

**Yearly aggregation with NULL exclusion**

```sql
SELECT
    STRFTIME('%Y', publish_date) AS year,
    COUNT(*) AS items_published,
    AVG(duration_seconds) AS avg_duration
FROM content_items
WHERE publish_date IS NOT NULL
GROUP BY STRFTIME('%Y', publish_date)
ORDER BY year
```

**Group on the raw date**

`GROUP BY publish_date` keeps one row per day, so the annual report becomes a daily log, and the undated rows still form their own NULL group.

**Group on the extracted year**

`GROUP BY STRFTIME('%Y', publish_date)` collapses the dated rows into one row per calendar year, the grain the report promises.

> **`SUM / COUNT(*)` is not the average here**
>
> About 20% of `duration_seconds` values in the sample are NULL. `SUM(duration_seconds) / COUNT(*)` keeps those items in the denominator and drags each affected year's mean down, while `AVG` divides only by the durations present. Reading a missing duration as zero is a business decision to confirm, not a default.

> **Name the NULL key before you are asked**
>
> Say out loud that a NULL `publish_date` would become its own group, and that `AVG` already ignores a NULL `duration_seconds`. It shows you reason about what the data holds, not only about what the query says.

> **One scan, five tiny buckets**
>
> At production scale the table holds about 500K rows. The plan is one sequential scan, a filter and a hash aggregate with a handful of keys. Memory stays flat because the aggregate keeps only one running count and sum per year.

---

## Common follow-up questions

- How would you show year-over-year growth in `items_published`? _(Read the previous year's count with `LAG(items_published) OVER (ORDER BY year)` and subtract.)_
- What changes if the report also splits by `content_type`? _(Add `content_type` to the grouping for a two-dimensional breakdown.)_
- How would you treat a missing `duration_seconds` as zero? _(`COALESCE(duration_seconds, 0)` inside the average changes the business meaning, so confirm it first.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/yearly_output)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.