Lighthouse, The
- O(n²) · Very hard
- Full plan
- SQL
- strings
- conditionals
- sorting
Problem
So that "The Lighthouse" turns up under L, the book's card in the catalog doesn't say "The Lighthouse." It says "Lighthouse, The": the article at the front moves to the end, after a comma and a space. Without that, half the card catalog would live under T and under A.
Three articles move: The, A, and An. They move only when one of them is the title's first word, the whole word. A title that doesn't start with one of them stays as it is.
Show each book's card and its year of publication, in that order, sorted alphabetically by card.
What it has to return
These rows, in this order
| card | published |
|---|---|
| Blue House, The | 2011 |
| Lighthouse, The | 1998 |
| Long Night | 2005 |
| Loose Leaves | 1987 |
| Maps of Salt | 2005 |
| Short Stories | 1975 |
| Three Winters | 2011 |
| Train South, The | 2019 |
This is what comes out with the data in the practice database this challenge uses. The column names don’t count, so name yours however you like: what counts is how many there are and the order they come in. Besides this one, the challenge has hidden tests with other data that are revealed when you submit your solution.
You start with this
SQL
SELECT title AS card, published
FROM books
ORDER BY card;It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.
More O(n²) challenges
- One raise, not twoSQL · changing data · conditionals
- Out of every 100 in payrollSQL · window functions · decimals
- Paige is leavingSQL · changing data · subqueries
- Past dueSQL · dates · nulls · joins
- Starting at the bottomSQL · changing data · subqueries
- The library forgetsSQL · changing data · subqueries