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.

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 DATE

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 3 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 3 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: Leave equal durations alone

The seed holds pairs of items with the same length: 4500 and 10004600 both run 4760 seconds. The ask is longest first and says nothing about equal lengths, so either of the pair may lead. Adding a second sort key would invent a rule nobody asked for.

Longest content first
SELECT content_id, title, duration_seconds
FROM content_items
ORDER BY duration_seconds DESC

Common follow-up questions