All problems

Where Do Customers Live?

easySQLGroupBy

Whetcode Supply is choosing somewhere to put a second warehouse, and the shortlist starts from the simplest signal available: how many registered customers live in each city. Shipping costs fall fastest beside the biggest cluster, so the busiest city needs to be at the top of the list.

Three of the four cities have the same number of customers, so the list also needs a rule for what happens when two cities tie — otherwise the same query produces a different shortlist on different runs and nobody can tell which one they read last week.

customers — one row per registered customer. city is the city given at signup and signup_date is the day they registered, written year-month-day.

id name city signup_date
1 Priya Nair Austin 2022-01-15
2 Tom Becker Berlin 2022-03-02
3 Sofia Rossi Milan 2022-05-19
4 Liam OConnor Dublin 2023-01-08
5 Wei Zhang Austin 2023-04-27

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

Task: Write a query returning two columns, city and n (how many registered customers give that city), one row per city that appears in the table, most customers first. When two cities have the same number, list them alphabetically by city. A city with no registered customers does not appear at all.

Example output — shape only, on invented cities. The second and third share a headcount, so they fall alphabetically:

city n
Lisbon 6
Osaka 3
Utrecht 3

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…