The Ones Who Hold Attention

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

Problem

How long do viewers stay with each creator's work, measured as the average watch_seconds across every view in content_views of the pieces that creator published in content_items? We want one figure per creator_id, with views of pieces that have no credited creator pooled into a single blank row.

Time on screen is the real vote. Find the creators earning it.

Tables

content_views

Columns: view_id INTEGER, content_id INTEGER, user_id INTEGER, device_id INTEGER, viewed_at TEXT, watch_seconds INTEGER

content_views: 10 of 200 rows
view_idcontent_iduser_iddevice_idviewed_atwatch_seconds
705337213614042026-02-02 01:07:0041
710650126226072026-03-03 02:14:0072
715963038838102026-04-04 03:21:00103
7212759514410132026-05-05 04:28:00134
7265888640512162026-06-06 05:35:00165
73181017766614192026-07-07 06:42:00196
73711146NULL16222026-08-08 07:49:00227
7424127548818252026-09-09 08:56:00258
747714041749NULL2026-01-10 09:03:00289
75301533301022312026-02-11 10:10:00320

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 51 rows
creator_idavg_watch_seconds
NULL1453.7142857142858
1001808
1972335
2942110.25
3911942.3333333333333

Worked solution and explanation

Under the attention-analytics costume, this is two-hop attribution. A row in content_views names a piece of content. It never names whose piece it is. That fact lives only in content_items. Anyone can write the GROUP BY and the AVG. What separates candidates is the bridge they pick. content_id is the real link. user_id against creator_id only looks like one. Pick the look-alike and every creator gets credited with their own browsing instead of with the attention their work earned.

The trap

Both tables carry an id that smells like a person: cv.user_id and ci.creator_id. Writing ON cv.user_id = ci.creator_id compiles, runs, and returns believable numbers. But the user_id on a view is the viewer. Equate it with a creator and you are measuring how long creators watch other people's work, which is the exact opposite of the question. The ids even overlap in value, so nothing errors and nothing looks empty.

The only real edge runs view to piece on `content_id`. The creator sits one hop further, on the piece.
  • content_views: view_id int PK, content_id int FK, user_id int, watch_seconds int
  • content_items: content_id int PK, creator_id int, title text
  • content_views.content_id references content_items.content_id

Step 1: Bridge each view to its piece on content_id

Inner-join content_items to content_views on cv.content_id = ci.content_id. content_id is unique in content_items, so each view picks up exactly one piece and one creator_id. Nothing fans out, which means no dedup worries anywhere downstream.

Step 2: Average the views per creator

Group by ci.creator_id and take AVG(cv.watch_seconds) over the raw views. AVG skips views with no recorded watch time, so an untimed view neither counts as zero seconds nor pads the denominator. Pieces with no credited creator all share a NULL creator_id, so their views collapse into the one blank row the statement asks to keep.

Average watch time per creator
SELECT ci.creator_id,
       AVG(cv.watch_seconds) AS avg_watch_seconds
FROM content_views cv
INNER JOIN content_items ci
       ON cv.content_id = ci.content_id
GROUP BY ci.creator_id
;

One hop to the piece, then collapse to the creator.

Linking on the viewer
ON cv.user_id = ci.creator_id pairs a creator with the views they themselves made. It also repeats each of those views once per piece the creator published. The numbers describe creators as an audience.
Linking on the piece
ON cv.content_id = ci.content_id pairs each view with the piece it was a view of, exactly once. The numbers describe the audience a creator's work held.

Common follow-up questions