I would approach this problem in 3 steps. First get an account of each book from each city.
select customers.city, books.title, count(books.title) as count from loaned, books, customers where loaned.userID = customers.userID and loaned.bookID = books.bookID group by customers.city, books.title
This query will return the following lines.
+------------+-------------------------------+-------+ | city | title | count | +------------+-------------------------------+-------+ | Harrogate | The cross rabbit | 1 | | Harrogate | PHP and MySQL web development | 2 | | Whitehaven | Greek Mythology | 1 | | Whitehaven | Dino-soaring | 2 | | Sale | Magic tricks | 3 | | Sale | Dino-soaring | 2 | +------------+-------------------------------+-------+
Using this data, I will use this to make a group for each city with the largest number.
select city, max(count) as count from ( select customers.city , books.title, count(books.title) as count from loaned, books, customers where loaned.userID = customers.userID and loaned.bookID = books.bookID group by customers.city, books.title ) as city_book_max_count group by city
What will return these lines
+------------+-------+ | city | count | +------------+-------+ | Harrogate | 2 | | Whitehaven | 2 | | Sale | 3 | +------------+-------+
Using the data from the two tables, we can join them by city and account to get the corresponding books that correspond to both tables.
select city_book_count.city, city_book_count.title from ( select customers.city , books.title, count(books.title) as count from loaned, books, customers where loaned.userID = customers.userID and loaned.bookID = books.bookID group by customers.city, books.title ) as city_book_count join ( select city, max(count) as count from ( select customers.city , books.title, count(books.title) as count from loaned, books, customers where loaned.userID = customers.userID and loaned.bookID = books.bookID group by customers.city, books.title ) as city_book_count_temp group by city ) as city_book_max_count on city_book_count.city = city_book_max_count.city and city_book_count.count = city_book_max_count.count