Session-Fit Content
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
- Amazon
Problem
For user 197, find content items whose duration is less than or equal to that user's average session length. Show the user ID, content ID, and content duration.
Content that fits the session length.
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 |
user_sessions
Columns: session_id INTEGER, user_id INTEGER, device_id INTEGER, session_start TEXT, session_duration_sec INTEGER, pages_viewed INTEGER
| session_id | user_id | device_id | session_start | session_duration_sec | pages_viewed |
|---|---|---|---|---|---|
| 7061 | 197 | 230 | 2026-02-02 01:00:00 | 53 | 3 |
| 7122 | 294 | 259 | 2026-03-03 02:00:00 | 76 | 6 |
| 7183 | 391 | 288 | 2026-04-04 03:00:00 | 99 | 9 |
| 7244 | 488 | 317 | 2026-05-05 04:00:00 | 122 | 12 |
| 7305 | 585 | 346 | 2026-06-06 05:00:00 | 145 | 15 |
| 7366 | 682 | 375 | 2026-07-07 06:00:00 | 168 | 18 |
| 7427 | 779 | 404 | 2026-08-08 07:00:00 | 191 | 21 |
| 7488 | 876 | NULL | 2026-09-09 08:00:00 | 214 | 24 |
| 7549 | 973 | 462 | 2026-01-10 09:00:00 | 237 | 27 |
| 7610 | 1070 | 491 | 2026-02-11 10:00:00 | 260 | 30 |
Expected output
| user_id | content_id | duration_seconds |
|---|---|---|
| 197 | 286 | 154 |
| 197 | 329 | 201 |
| 197 | 372 | 248 |
| 197 | 415 | 295 |
| 197 | 501 | 389 |
Worked solution and explanation
Why this problem exists in real interviews
Drawn from a session analysis domain, this question centers on filtering and projection over the content_items and user_sessions tables. The tricky part is handling columns like content_id, duration_seconds, and user_id correctly under the given constraints.
Break down the requirements
Step 1: Apply the range filter
The WHERE clause restricts rows to the target range. Applying this filter early reduces the volume flowing into downstream operations.
Step 2: Use a subquery to find the reference value
The scalar subquery computes a single value (like the maximum) that the outer query filters against. This avoids a self-join.
The solution
SELECT 197 AS user_id, ci.content_id, ci.duration_seconds
FROM content_items ci
WHERE ci.duration_seconds <= (
SELECT AVG(session_duration_sec)
FROM user_sessions
WHERE user_id = 197
)Common follow-up questions
- What result would you get if every value in `content_items.duration_seconds` were NULL? Would your query return an empty set or something unexpected? (Tests extreme NULL scenarios and whether the candidate guards against edge cases in `duration_seconds`.)
- `user_sessions.device_id` has roughly 6,000,000 distinct values. What index strategy would you use to avoid a full scan on `user_sessions`? (Tests indexing knowledge specific to the high-cardinality `device_id` column in `user_sessions`.)
- `content_items.content_type` only has 10 distinct values. If a new category were added, would your query automatically include it? (Tests whether the query hard-codes values or dynamically adapts to `content_type` changes.)