Yearly Output

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

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.

Publishing velocity for the board deck.

Tables

content_items

Columns: content_id INTEGER, title TEXT, content_type TEXT, duration_seconds INTEGER, creator_id INTEGER, publish_date DATE

content_items: 10 of 200 rows
content_idtitlecontent_typeduration_secondscreator_idpublish_date
243The Ultimate Guide to AI TrendsarticleNULL1972026-02-02
286Top 10 Mobile Appspodcast1542942025-03-03
329Understanding Machine Learning [DRAFT]short2013912026-04-04
372Mastering Tech Startupslivestream2484882025-05-05
415Introduction to Cybersecurityvideo2955852026-06-06
458Deep Dive: Cloud ComputingarticleNULL6822025-07-07
501Behind the Scenes of Product Managementpodcast3897792026-08-08
544Breaking Down DevOpsshort4368762025-09-09
587Exploring Web Developmentlivestream483NULL2026-01-10
630How to Data Sciencevideo53010702025-11-11

Expected output

First 5 of 5 rows
yearitems_publishedavg_duration
202220NULL
2023202386.5
2024202433.5
2025672442.9824561403507
2026612492.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 5 rows after the aggregate, so it costs nothing.

The solution

Yearly aggregation with NULL exclusion
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.

Common follow-up questions