Titles Ending With S
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
Pull all content items whose title ends with the letter 's', listed alphabetically by title so the catalog reads in order. For each item show the content ID, title, content type, duration in seconds, creator ID, and publish date, and when two share a title put the lower content ID first.
Naming conventions. Specifically the plurals.
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 | content_type | duration_seconds | creator_id | publish_date |
|---|---|---|---|---|---|
| 544 | Breaking Down DevOps | short | 436 | 876 | 2025-09-09 |
| 974 | Breaking Down DevOps | short | 906 | NULL | 2025-07-19 |
| 372 | Mastering Tech Startups | livestream | 248 | 488 | 2025-05-05 |
| 243 | The Ultimate Guide to AI Trends | article | NULL | 197 | 2026-02-02 |
| 286 | Top 10 Mobile Apps | podcast | 154 | 294 | 2025-03-03 |
Worked solution and explanation
What this really tests
This is a suffix match dressed up as a catalog pull. Everyone reaches for LIKE; what separates candidates is where the wildcard goes. '%s%' asks whether an 's' appears anywhere, and on content_items that drags in 'Introduction to Cybersecurity' and 'Behind the Scenes of Product Management', so the 80-row answer quietly swells and still looks plausible. The second signal is what you ask before typing: is the match case-sensitive, and are the titles trimmed?
Break down the requirements
Step 1: Pick the suffix predicate
Filter content_items.title with LIKE '%s'. The leading % says any prefix, the literal s pins the final character.
Step 2: Nail case sensitivity
SQLite's LIKE is ASCII case-insensitive by default; Postgres LIKE is case-sensitive. Ask the interviewer which engine. Portable form: title LIKE '%s' OR title LIKE '%S', or normalize with LOWER(title) LIKE '%s'.
Step 3: Defend against trailing whitespace
'Articles ' LIKE '%s' is false because the last character is a space. If content_items.title is dirty, wrap in TRIM(title) LIKE '%s'. State the assumption either way.
Step 4: Project the asked columns and sort
Name content_id, title, content_type, duration_seconds, creator_id, publish_date explicitly rather than SELECT *. Then ORDER BY title, content_id: the catalog reads A to Z, and repeated titles such as 'Breaking Down DevOps' list the lower content_id first.
The solution
SELECT content_id,
title,
content_type,
duration_seconds,
creator_id,
publish_date
FROM content_items
WHERE title LIKE '%s'
ORDER BY title, content_id;COMMON FOLLOW-UP QUESTIONS
Common follow-up questions
- How would you make this query case-insensitive in Postgres? (Probes ILIKE vs `LOWER(title) LIKE '%s'`, and the index implications of wrapping the column in a function (need an expression index).)
- The column has trailing whitespace on 12% of rows. How do you handle it? (Tests whether you reach for `TRIM(title) LIKE '%s'` at query time or push a cleanup upstream into the ingest job.)
- If `content_items` grew to 10 billion rows, how would you make suffix lookup fast? (Looks for a generated `last_char` column with an index, or a reversed-string column converting suffix search into prefix search.)
- How would you count titles ending in each letter a through z in one query? (Probes `GROUP BY SUBSTR(title, -1)` (or `RIGHT(title, 1)`) plus `LOWER` for case folding, in a single scan instead of 26 LIKE filters.)