All problems

Cities With More Than One Customer

mediumSQLGroupByHaving

Whetcode Supply is testing local pickup points, and the rule for opening one is blunt: a city needs at least two registered customers before it is worth the rent. Ops wants the shortlist, with the customer tally beside each city so they can weigh up the candidates by hand afterwards.

That tally is the awkward part. It is not stored in the table at all — it comes into existence only once the customers have been bundled together by city. And the test "more than one" has to be applied to that bundled figure, not to a single customer row: no individual customer knows, or could know, how many other people share their city.

customers — one row per registered customer. city is the city given at signup, signup_date is an ISO date string.

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 num_customers, with one row per city that has more than one registered customer. Row sequence does not matter.

Example output — shape only, on invented cities and counts.

city num_customers
Lisbon 4
Osaka 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…