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_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
| 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
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
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 yearCommon follow-up questions
- How would you show the year-over-year growth in items published? (Use LAG to access the previous year's count and compute the difference.)
- What if you needed to break this down by `content_type` as well? (Add `content_type` to the GROUP BY for a two-dimensional breakdown.)
- How would you handle items published on the same day with different durations? (The aggregation handles this naturally; each item is counted individually.)