Engagement by Content Type

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
Asked in
Yelp

Problem

Articles have no recorded running time in content_items, yet we still list them alongside the timed formats. For each content type, give its total duration in seconds and how many items it has, longest total first.

Some content types get all the attention.

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
content_typetotal_durationitem_count
video10110040
livestream9922040
short9734040
podcast9546040
article040

Worked solution and explanation

What this really is

Strip the costume and this is a NULL-preservation problem dressed up as a format report. Grouping by content_type and summing is the easy 80 percent. The real test is the article format: every one of its 40 rows has a NULL duration_seconds, so a bare SUM returns NULL and COUNT(duration_seconds) counts none of them. Miss that and articles either vanish behind a filter, sit at NULL, or report zero items. They should read total 0 with their full count of 40.

Working through it

Step 1: Collapse content_items to one row per content_type

Five formats means five output rows. Resist any WHERE duration_seconds IS NOT NULL: it looks like hygiene, and it deletes the article format outright.

Step 2: Default the sum, not the rows

SUM skips NULLs, which is fine for the timed formats. For a group where every value is NULL, though, it returns NULL instead of 0. Wrap it as COALESCE(SUM(duration_seconds), 0) so articles land on a real zero.

Step 3: Count rows, then sort by the total

COUNT(*) counts items; COUNT(duration_seconds) counts durations, which gives articles 0. Then sort by total_duration descending. The five totals all differ, so the order is fully decided without any tie rule.

The solution

Total duration per content type
SELECT content_type,
       COALESCE(SUM(duration_seconds), 0) AS total_duration,
       COUNT(*) AS item_count
FROM content_items
GROUP BY content_type
ORDER BY total_duration DESC
Bare aggregates
SUM(duration_seconds) and COUNT(duration_seconds) leave the article row as NULL and 0. That reads as a format nobody published.
Defaulted sum, counted rows
COALESCE(SUM(duration_seconds), 0) and COUNT(*) give the article row 0 and 40: published, just never timed.

Common follow-up questions