Skip to the content

The eighties, the nineties

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

decadetitle
1970Short Stories
1980Loose Leaves
1990The Lighthouse
2000Long Night
2000Maps of Salt
2010The Blue House
2010The Train South
2010Three 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;
Solve this challenge

It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.

More O(log n) challenges

See all challenges →