Their latest, front and center
- O(n log n) · Hard
- Full plan
- SQL
- subqueries
- joins
Problem
The librarian is setting up a display by the front desk with the latest from each author: the book of theirs that was published most recently. The library stores only the year a book was published, so if an author's two most recent books are from the same year, both go on the display. Show the author's name, the title, and the year it was published, in that order, sorted by author and, within an author, by title. The book with no author isn't anyone's latest and stays on its shelf, and an author the library has no books by has nothing to put out.
What it has to return
These rows, in this order
| name | title | published |
|---|---|---|
| Erin Reilly | Maps of Salt | 2005 |
| Martha Sloan | The Train South | 2019 |
| Patrick Lyle | Long Night | 2005 |
| Tom Vance | The Blue House | 2011 |
| Tom Vance | Three Winters | 2011 |
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 author_id,
MAX(published) AS latest
FROM books
GROUP BY author_id;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.