The eighties, the nineties
- O(log n) · Easy
- Free
- SQL
- operations
- sorting
Problem
The librarian is going to arrange a shelf by decade, with a label on each stretch: 1970, 1980, 1990, and on up as far as the books go. A book from 1987 goes under the 1980 label, and one from 1990 is the very first under the 1990 label. books has only each book's year, and she doesn't want a list of ranges typed in by hand that needs a new line every ten years.
Show the label each book goes under and its title, in that order. Sort the rows from the earliest label to the latest and, under each label, by title.
What it has to return
These rows, in this order
| decade | title |
|---|---|
| 1970 | Short Stories |
| 1980 | Loose Leaves |
| 1990 | The Lighthouse |
| 2000 | Long Night |
| 2000 | Maps of Salt |
| 2010 | The Blue House |
| 2010 | The Train South |
| 2010 | Three Winters |
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 published, title
FROM books
ORDER BY published, title;It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.