SQL QuestSQL Interview QuestionsString Functions

Shelve the Films Library-Style

HardProQuerying BasicsString FunctionsConditional LogicSubqueries & CTEsAggregation & Grouping

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

idtitleyeargenreratingvotesrevenue_millionsruntimedirector
1Guardians of the Galaxy2014Action8.1757074333.13121James Gunn
2Prometheus2012Adventure7485820126.46124Ridley Scott
3Split2016Horror7.3157606138.12117M. Night Shyamalan

Expected output: 23 rows — S holds 17 films, 3 of them moved there by the rule

Hint

This is a Pro challenge — the hint, the step-by-step tutor and the reference solution open in the app.

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

Names That Don't Match Their EmailMedium · FreePassenger Family Survival AnalysisHard · ProHer Own First NameHard · ProLong-Named PassengersEasy · FreeEmployee Display Cards (Concat)Easy · FreeCountry Codes in UppercaseEasy · Free

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