Where are the books from?
- O(n) · Medium
- Full plan
- SQL
- grouping
- joins
- sorting
Problem
A neighbor at the front desk asks where the library's books are from. For each country with books on the shelves, show the country, how many of its authors have a book in the library, and how many pages those books add up to, in that order. The country with the most pages goes first, and ties go in alphabetical order.
The book with no author isn't from any country, and an author with no books doesn't put a country on the list.
What it has to return
These rows, in this order
| country | how_many | total |
|---|---|---|
| Ireland | 2 | 860 |
| USA | 1 | 600 |
| Canada | 1 | 355 |
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 a.country, b.pages
FROM books b
JOIN authors a
ON b.author_id = a.idIt 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.