Same book twice doesn't count
- O(n) · Medium
- Full plan
- SQL
- grouping
- sorting
Problem
The library is giving a bookmark to every member who has borrowed more than one book. That means different books: someone who borrowed the same one twice has read one.
Show the member and how many different books they've borrowed, from the most to the fewest. Ties go in alphabetical order.
What it has to return
These rows, in this order
| member | titles |
|---|---|
| Ann | 3 |
| Luke | 2 |
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 member, COUNT(*)
FROM loans
GROUP BY memberIt 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
- The 250 ClubSQL · grouping · joins · decimals
- The notebook by the registerSQL · grouping · joins
- What the team makesSQL · grouping · joins · decimals
- Where are the books from?SQL · grouping · joins · sorting
- Where do our orders come from?SQL · grouping · joins · nulls
- Where there's more than oneSQL · grouping · nulls