Skip to the content

Where are the books from?

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

countryhow_manytotal
Ireland2860
USA1600
Canada1355

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.id
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 →