The Long Watch

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
L4

Problem

List every content type in content_items with how many items it has published and its average runtime, longest-running formats first. Articles never record a runtime, but the media team's library audit still counts them as published.

Some formats hold you longer than others. Measure which ones.

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

products

Columns: product_id INTEGER, product_name TEXT, category TEXT, price REAL, rating REAL, in_stock INTEGER

products: 10 of 200 rows
product_idproduct_namecategorypriceratingin_stock
1001Basic Device 1XBooks17.522.71
1050Deluxe Bundle 2XClothing25.054.41
1099Pro Unit 3XHome & Kitchen32.582.11
1148Ultra Tool 4XCollectibles40.113.81
1197Essential Set 5XToys47.641.51
1246Classic System 6XBeauty03.21
1295Smart Gadget 7XAutomotive62.74.90
1344Eco Kit 8XGarden70.232.61
1393Turbo Pack 9XMusic77.764.30
1442Premium Widget 10XElectronics85.2921

Expected output

First 5 of 5 rows
content_typeitems_publishedavg_runtime
video402527.5
livestream402480.5
short402433.5
podcast402386.5
article40NULL

Worked solution and explanation

What this really is

Beneath the 'audit the library' framing, this is a two-population report wearing the costume of a single grouped query. items_published counts every row in a content type; avg_runtime only ever sees the rows that actually have a runtime. Articles hold both facts at once: 40 of them were published, and not one is timed. The candidates who trip reach for one filter to serve both numbers. They add WHERE duration_seconds IS NOT NULL to tidy up the average, and that same clause deletes all 40 articles before the count runs, so the article row vanishes from the report. Get that wrong and the 'published' column stops meaning published.

The trap in one line

Filter first (wrong)
Adding WHERE duration_seconds IS NOT NULL before grouping keeps the average clean, but every article row is gone before the count runs. The format drops out of the report entirely, and the audit shows four formats instead of five.
Trust NULL semantics (right)
Leave every row in. AVG ignores the NULL runtimes on its own, so article survives with items_published = 40 and an empty avg_runtime, and every count stays honest.
Both metrics, one pass
SELECT
    content_type,
    COUNT(*) AS items_published,
    AVG(duration_seconds) AS avg_runtime
FROM content_items
GROUP BY content_type
ORDER BY avg_runtime DESC;

COUNT(*) sees all rows; AVG ignores the NULL runtimes; no WHERE needed.

Step 1: Break the catalog down by format

GROUP BY content_type gives one row per format. Everything else is a metric computed over the rows inside each group, so the shape of the answer is settled before you write a single aggregate.

Step 2: Count every item, average only the timed ones

COUNT(*) deliberately includes the untimed articles, because they were still published. AVG(duration_seconds) deliberately excludes them, because there is no runtime to average. The asymmetry is the point, and you get it for free by not filtering.

Step 3: Order by the average, longest first

ORDER BY avg_runtime DESC puts the longest formats on top. The article group has a NULL average, and NULLs sort to the bottom under a descending order here, which is exactly where an untimed format belongs on a runtime leaderboard.

One more thing worth noticing: the products table is in scope but never touched. Real schemas hand you more than you need, and part of the job is recognizing which tables the question actually depends on. Nothing about runtime by format lives in products, so it stays out of the query.

Common follow-up questions