All problems

Branch Planning

easySQLGroupBy

Northline Bank is deciding which branch to open next, and the starting point is the boring one: how many customers each city already holds. A city carrying a crowd is a case for a second branch; a city carrying one person is a case for closing the first.

customers — one row per account holder at Northline. account_type is either checking or savings; city is the branch city the account belongs to.

id name account_type city
1 Farah Idris checking Chicago
2 Grant Boyle savings Miami
3 Hana Suzuki checking Chicago
4 Ibrahim Njoku savings Miami
5 Jade Wu checking Seattle

The table already exists in the database — there is nothing to create or load.

Task: Write a query that returns one row per city, with columns city and n, n being how many customers belong to that city. Busiest city first; two cities holding the same number of customers come back in alphabetical order of city.

Example output — shape only, on invented cities. The first two share a headcount, so they fall alphabetically:

city n
Denver 9
Portland 9
Tulsa 4

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…