Content by Specific Users
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
Problem
Which pieces of content in content_items were authored by the accounts 'alice', 'aaron42' and 'ava99'? A reviewer auditing those three creators needs each item's content ID, title, content type and creator ID, sorted by content ID.
Two creators. What did they publish?
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 |
users
Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT
| user_id | username | signup_date | account_status | age_bucket | |
|---|---|---|---|---|---|
| 100 | alice | alice@example.com | 2025-02-02 | inactive | 25-34 |
| 197 | aaron42 | aaron42@example.com | 2026-03-03 | suspended | 35-44 |
| 294 | amelia | amelia@example.com | 2024-04-04 | pending_verification | 45-54 |
| 391 | arjun | arjun@example.com | 2025-05-05 | active | 55-64 |
| 488 | ava99 | ava99@example.com | 2026-06-06 | inactive | 65+ |
| 585 | andrew_k | andrew_k@example.com | 2024-07-07 | suspended | NULL |
| 682 | anika | anika@example.com | 2025-08-08 16:34:00 | pending_verification | 18-24 |
| 779 | aiden | aiden@example.com | 2026-09-09 14:53:00 | active | 25-34 |
| 876 | aria_b | aria_b@example.com | 2024-10-10 | inactive | 35-44 |
| 973 | brian | brian@example.com | 2025-11-11 | suspended | 45-54 |
Expected output
| content_id | title | content_type | creator_id |
|---|---|---|---|
| 243 | The Ultimate Guide to AI Trends | article | 197 |
| 372 | Mastering Tech Startups | livestream | 488 |
| 2350 | How to Data Science | video | 100 |
| 2393 | The Ultimate Guide to AI Trends | article | 197 |
| 10004504 | Mastering Tech Startups | livestream | 488 |
Worked solution and explanation
What this is really asking
Strip off the audit costume and this is a name-to-id lookup: a filter whose predicate lives in a different table from the rows you return. content_items only knows a creator as a numeric creator_id; the names 'alice', 'aaron42' and 'ava99' live one table over, in users.username. Anyone can write a SELECT with a filter. What separates answers is realizing you cannot filter content_items by name at all, because the name is not in that table. Skip the bridge and you either hit a missing column or hardcode ids you read off the screen, and those quietly pick up the wrong creators the day the ids move.
- content_items: content_id PK, title, content_type, creator_id FK, publish_date
- users: user_id PK, username, account_status
- content_items.creator_id references users.user_id
Step 1: Bridge content to its creator
Join content_items to users on ci.creator_id = u.user_id. An inner join is exactly right: content rows with a NULL creator_id (like 'Exploring Web Development') belong to nobody, and they drop out because NULL never equals anything.
Step 2: Filter by the three names, not three guessed ids
Apply WHERE u.username IN ('alice', 'aaron42', 'ava99'). Matching on the identifier the reviewer handed you keeps the query right even if ids are reassigned. Exact match is the point: 'Alice' with a capital A is a different account, and it stays out.
Step 3: Project from the content side and sort
Return ci.content_id, ci.title, ci.content_type and ci.creator_id, then sort by ci.content_id ascending. content_id is unique, so the order is fully determined and two correct answers cannot disagree.
SELECT ci.content_id, ci.title, ci.content_type, ci.creator_id
FROM content_items ci
JOIN users u ON ci.creator_id = u.user_id
WHERE u.username IN ('alice', 'aaron42', 'ava99')
ORDER BY ci.content_idJoin to where the names live, filter there, project from content_items.
Common follow-up questions
- How would you also exclude content from creators whose `account_status` is 'suspended'? (A second predicate on the joined `users` side; 'aaron42' is suspended, so the result shrinks.)
- What changes if a username was stored with stray spaces, like ' amelia'? (Tests whether you normalize the stored key before matching instead of trusting the text as written.)
- How would you list the named creators who have zero items? (Pushes toward an outer join or anti-join, the inverse of the inner-join shape used here.)