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_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

users

Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

Expected output

First 5 of 10 rows
content_idtitlecontent_typecreator_id
243The Ultimate Guide to AI Trendsarticle197
372Mastering Tech Startupslivestream488
2350How to Data Sciencevideo100
2393The Ultimate Guide to AI Trendsarticle197
10004504Mastering Tech Startupslivestream488

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.

`creator_id` is a foreign key into `users.user_id`; the names you filter on live only in `users`.
  • 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.

Canonical solution
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_id

Join to where the names live, filter there, project from content_items.

Common follow-up questions