Year-over-Year Content Launches
A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- medium
- Seniority
- L3
- Asked in
- Tesla
Problem
A content analyst noticed the year-over-year output figures for the platform's video side are inflated by articles and podcasts. Rebuild them from content_items counting only video, short and livestream items: every creator who has ever published one gets a line with their 2026 count minus their 2025 count. Items with no creator on record stay together as one uncredited line.
Launch velocity, year over year.
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
| creator_id | net_difference |
|---|---|
| NULL | -2 |
| 100 | 0 |
| 391 | 2 |
| 488 | -2 |
| 585 | 4 |
Worked solution and explanation
Two filters that look alike
This is a pivot of two years into one signed number, dressed up as a report cleanup. Anyone can write the two conditional sums. What separates candidates is where each restriction goes: the format rule belongs in WHERE because it decides which items exist, while the years belong inside the CASE because they only decide which side of the subtraction an item lands on. Push the years into WHERE and every creator whose video output sat in earlier years vanishes instead of showing a flat zero.
Step 1: Throw out the non-video items first
WHERE content_type IN ('video', 'short', 'livestream') removes articles and podcasts before anything is counted. That is the whole reason the old figures were wrong, so it is a row filter, applied once.
Step 2: Bucket each item by year inside the sum
SUM(CASE WHEN STRFTIME('%Y', publish_date) = '2026' THEN 1 ELSE 0 END) counts this year's items; the twin with '2025' counts last year's. Items from other years add 0 to both, which is exactly what lets a flat creator stay on the list.
Step 3: Subtract in one pass per creator
GROUP BY creator_id and subtract the two sums. No join, no second scan, and the NULL creator falls out as its own group: that is the uncredited line.
SELECT
creator_id,
SUM(CASE WHEN STRFTIME('%Y', publish_date) = '2026' THEN 1 ELSE 0 END)
- SUM(CASE WHEN STRFTIME('%Y', publish_date) = '2025' THEN 1 ELSE 0 END) AS net_difference
FROM content_items
WHERE content_type IN ('video', 'short', 'livestream')
GROUP BY creator_idSELECT
creator_id,
SUM(CASE WHEN STRFTIME('%Y', publish_date) = '2026' THEN 1 ELSE 0 END)
- SUM(CASE WHEN STRFTIME('%Y', publish_date) = '2025' THEN 1 ELSE 0 END) AS net_difference
FROM content_items
WHERE content_type IN ('video', 'short', 'livestream')
AND STRFTIME('%Y', publish_date) IN ('2025', '2026')
GROUP BY creator_idYears in WHERE drop every video creator with nothing in the two years.
- Row filter
content_type IN (...)inWHERE: decides whether an item counts at all.- Bucket condition
- The year test inside
CASE: decides which count an item feeds, never whether its creator appears.
Common follow-up questions
- How would you show the 2026 and 2025 counts as their own columns next to the difference? (Each conditional sum becomes a named column.)
- What if the report wanted percentage change instead of the absolute difference? (Divide by last year's count, guarding the zero case with `NULLIF`.)
- How would you generalize this to every pair of consecutive years? (Count per creator per year, then compare each year to the previous one.)