SQL Quest › SQL Interview Questions › String Functions
Shelve the Films Library-Style
Libraries and film catalogues file titles without their leading article: *The Dark Knight* sits under D, *A Beautiful Mind* would sit under B. The catalogue team wants to see how the 100 films in movies spread across the shelves once that rule is applied.
The rule: a title that starts with the word The, A or An — the word, followed by a space — is shelved by what comes after the article. Every other title is shelved by its own first character.
Show exactly these 3 columns, in this order: shelf (the first character of the shelving title, upper-cased), films (how many films sit on that shelf) and moved_by_article (how many of those films are there only because an article was dropped). Order by shelf.
The traps are all in the data:
- American History X, Arrival and Assassin's Creed start with the *letter* A, not the *word* A. A test for titles starting with 'A' files American History X under M.
- Trolls and Tarzan start with T, not with The.
- Resident Evil: The Final Chapter and Star Wars: The Force Awakens contain "The " in the middle. Remove every "The " from a title and both will count as moved — neither is.
Only the start of the title counts, and only a whole word.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
movies
| id | title | year | genre | rating | votes | revenue_millions | runtime | director |
|---|---|---|---|---|---|---|---|---|
| 1 | Guardians of the Galaxy | 2014 | Action | 8.1 | 757074 | 333.13 | 121 | James Gunn |
| 2 | Prometheus | 2012 | Adventure | 7 | 485820 | 126.46 | 124 | Ridley Scott |
| 3 | Split | 2016 | Horror | 7.3 | 157606 | 138.12 | 117 | M. Night Shyamalan |
Expected output: 23 rows — S holds 17 films, 3 of them moved there by the rule
Hint
Concepts
SELECT String Functions SUBSTR UPPER LIKE CASE CTE GROUP BY Aggregation
Practise the topic: SQL practice questions · String function practice · CASE WHEN practice · CTE practice · GROUP BY exercises · Advanced SQL interview questions
Read the concept: SQL cheat sheet
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test