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

The editorial director is building the annual content report and wants to see publishing velocity broken down by year. For each calendar year, show the number of items published and the average duration in seconds. Exclude any items that have no publish date on file. Present the years in chronological order.

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 TEXT

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

Why this problem exists in real interviews

This tests date extraction, NULL filtering, and multi-aggregate GROUP BY. Interviewers check whether you can derive a time dimension from a date column, exclude incomplete records, and compute multiple aggregates.


Break down the requirements

Step 1: Exclude items without a publish date

WHERE publish_date IS NOT NULL removes the 5% of items with no publish date.

Step 2: Extract year and aggregate

GROUP BY STRFTIME('%Y', publish_date) with COUNT(*) and AVG(duration_seconds) computes items published and average duration per year.

Step 3: Order chronologically

ORDER BY year presents the years from earliest to most recent.


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

Common follow-up questions