Content Duration Snapshot

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
Amadeus

Problem

An editor lining up homepage features by length asks one thing: which items in content_items run longest? She wants every item's ID, title and duration in seconds, longest first, with the lower ID ahead whenever two items run exactly as long.

A popularity snapshot by duration.

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 200 rows
content_idtitleduration_seconds
4500How to Data Science4760
10004600How to Data Science4760
4457Exploring Web Development4713
10004599Exploring Web Development4713
4414Breaking Down DevOps4666

Worked solution and explanation

What this really tests

Strip off the homepage story and this is a contract about row order: the answer is only right if the sequence is right. Everyone projects the three columns. What separates candidates is whether they trust the table to come back longest-first, or write ORDER BY duration_seconds and quietly get the ascending default. Either way the editor's list opens with the shortest clips, or with whatever order storage happened to return, and an order-graded comparison fails on the very first row.

Step 1: Project exactly the three columns asked for

The editor needs content_id, title and duration_seconds. Do not add a WHERE: every item belongs on the list, including the drafts and the ones whose length was never recorded.

Step 2: Sort duration_seconds descending, explicitly

A relational result is a set until you sort it. DESC is the whole question here: without it you get the shortest content on top.

Step 3: Settle exact ties on content_id

The seed holds pairs of items with the same length: 4500 and 10004600 both run 4760 seconds. Without a second key their relative order is up to the engine, so content_id ascending puts the lower ID ahead every time.

Longest content first, lower ID on ties
SELECT content_id, title, duration_seconds
FROM content_items
ORDER BY duration_seconds DESC, content_id

Common follow-up questions