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. Its columns are `customer_id` (INTEGER, primary key, unique customer identifier), `city` (VARCHAR(100), the city the customer lives in, never NULL) and `country` (VARCHAR(100), the country of that city, never NULL). The table has 1 to 100,000 rows. 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. 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. Output columns, in this order: `city`, `country`. Return one row per qualifying city, sorted by `city` ascending, then `country` ascending. No numeric column is returned, so no rounding applies.
Tables
customers(customer_id INTEGER, city VARCHAR(100), country VARCHAR(100))
Hints
- Two levels of aggregation: you need one number per city first. The threshold is an average of those numbers, one value per city.
- A city is identified by its (city, country) pair, both for the per-city counts and for the average.