Skip to the content

Lighthouse, The

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

cardpublished
Blue House, The2011
Lighthouse, The1998
Long Night2005
Loose Leaves1987
Maps of Salt2005
Short Stories1975
Three Winters2011
Train South, The2019

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;
Solve this challenge

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

See all challenges →