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_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_id | title | duration_seconds |
|---|---|---|
| 4500 | How to Data Science | 4760 |
| 10004600 | How to Data Science | 4760 |
| 4457 | Exploring Web Development | 4713 |
| 10004599 | Exploring Web Development | 4713 |
| 4414 | Breaking Down DevOps | 4666 |
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.
SELECT content_id, title, duration_seconds
FROM content_items
ORDER BY duration_seconds DESC, content_idCommon follow-up questions
- The homepage only has room for ten items. What changes? (A `LIMIT 10` after the sort, and whether a tie at the cut needs the `content_id` rule to decide who makes it.)
- How would you show each item's position on the list? (A `ROW_NUMBER()` over the same ordering, or `RANK()` if equal durations should share a position.)
- The product team wants items with no recorded length shown first so editors can fix them. How? (An explicit `NULLS FIRST` on `duration_seconds`, which also makes the query portable across engines.)