Find Cities With More Customers Than the Average per City
Quick Overview
Write a SQL query that finds every city with more customers than the average number of customers per city. It tests two-level aggregation, where grouped counts are compared against the average of those counts, along with strict threshold handling and a deterministic sort.
Find Cities With More Customers Than the Average per City
Company: Swiggy
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `customers` table stores one row per customer, with the city and country the customer lives in. Return the `city` and `country` of every city whose number of customers is greater than the average number of customers per city across the whole table.
```hint Two levels of aggregation
You need one number per city first. The threshold is an average of those numbers, one value per city.
```
### Input Table(s)
`customers`
| Column | Type | Meaning |
|---|---|---|
| customer_id | INT | Unique customer identifier |
| city | VARCHAR(100) | City the customer lives in |
| country | VARCHAR(100) | Country of that city |
Primary key: `customer_id`.
### Output Contract
- Columns, in this order: `city`, `country`.
- One row per qualifying city.
- Sort by `city` ascending, then `country` ascending.
- No numeric column is returned, so no rounding applies.
### Example
Input `customers`:
| customer_id | city | country |
|---|---|---|
| 1 | New York | USA |
| 2 | New York | USA |
| 3 | New York | USA |
| 4 | Paris | France |
| 5 | Tokyo | Japan |
Expected output:
| city | country |
|---|---|
| New York | USA |
The counts per city are New York 3, Paris 1 and Tokyo 1. The average is 5 / 3, about 1.67, and only New York is above it.
### Constraints and Clarifications
- The table has 1 to 100,000 rows.
- `customer_id` is unique, and `city` and `country` are never NULL.
- A city is identified by its (`city`, `country`) pair. Two rows with the same city name in different countries belong to two different cities, both for the counts and for the average.
- The average is taken over cities: one customer count per city, then the mean of those counts. A city with no customers has no rows, so it does not enter the average.
- The comparison is strict. A city whose count equals the average is left out, so when every city has the same count the result is empty.
Overview: Write a SQL query that finds every city with more customers than the average number of customers per city. It tests two-level aggregation, where grouped counts are compared against the average of those counts, along with strict threshold handling and a deterministic sort.
The customers table stores one row per customer, with the city and country the customer lives in. Return the city and country of every city whose number of customers is greater than the average number of customers per city across the whole table.
Input Table(s)
customers
Column
Type
Meaning
customer_id
INT
Unique customer identifier
city
VARCHAR(100)
City the customer lives in
country
VARCHAR(100)
Country of that city
Primary key: customer_id.
Output Contract
Columns, in this order:
city
,
country
.
One row per qualifying city.
Sort by
city
ascending, then
country
ascending.
No numeric column is returned, so no rounding applies.
Example
Input customers:
customer_id
city
country
1
New York
USA
2
New York
USA
3
New York
USA
4
Paris
France
5
Tokyo
Japan
Expected output:
city
country
New York
USA
The counts per city are New York 3, Paris 1 and Tokyo 1. The average is 5 / 3, about 1.67, and only New York is above it.
Constraints and Clarifications
The table has 1 to 100,000 rows.
customer_id
is unique, and
city
and
country
are never NULL.
A city is identified by its (
city
,
country
) pair. Two rows with the same city name in different countries belong to two different cities, both for the counts and for the average.
The average is taken over cities: one customer count per city, then the mean of those counts. A city with no customers has no rows, so it does not enter the average.
The comparison is strict. A city whose count equals the average is left out, so when every city has the same count the result is empty.