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_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
| content_type | total_duration | item_count |
|---|---|---|
| video | 101100 | 40 |
| livestream | 99220 | 40 |
| short | 97340 | 40 |
| podcast | 95460 | 40 |
| article | 0 | 40 |
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
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)andCOUNT(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)andCOUNT(*)give the article row 0 and 40: published, just never timed.
Common follow-up questions
- If we also report average duration per item, what should articles show? (`AVG` ignores NULLs, so the choice between NULL and 0 becomes a business decision.)
- `publish_date` is missing on some items: where do they go when you add a year dimension? (Tests NULL handling in the grouping key, not just in the measure.)
- How would you show a format that has no items at all? (Needs a list of formats to outer-join from, since `content_items` cannot produce a group with no rows.)