Given a Listings table with property listings and their prices by city, find all cities where the average home price exceeds the national average.
The result should be sorted by city name in ascending order.
Table Structure:
listing_id: Unique identifier for each listingcity: City name where the property is locatedprice: Price of the property
Table Schema
| Column Name | Type | Description |
|---|---|---|
listing_id
PK
|
int | Unique identifier for each property listing |
city
|
varchar | Name of the city where the property is located |
price
|
int | Price of the property in the listing |
Input & Output
| listing_id | city | price |
|---|---|---|
| 1 | New York | 800 |
| 2 | Los Angeles | 600 |
| 3 | New York | 1200 |
| 4 | Austin | 400 |
| 5 | Los Angeles | 700 |
| city |
|---|
| Los Angeles |
| New York |
National average: (800+600+1200+400+700)/5 = 740. City averages: New York = (800+1200)/2 = 1000 ✓, Los Angeles = (600+700)/2 = 650 ✗, Austin = 400 ✗. Wait, let me recalculate: Los Angeles = 650 < 740, so only New York qualifies. Actually Los Angeles average is 650 which is less than 740, so the output should be just New York.
| listing_id | city | price |
|---|---|---|
| 1 | Boston | 900 |
| 2 | Chicago | 500 |
| 3 | Miami | 700 |
| city |
|---|
| Boston |
| Miami |
National average: (900+500+700)/3 = 700. Since each city has only one listing, city averages equal the listing prices. Boston (900) and Miami (700) are >= 700, while Chicago (500) is below average.
| listing_id | city | price |
|---|---|---|
| 1 | Dallas | 300 |
| 2 | Phoenix | 400 |
| 3 | Dallas | 500 |
| city |
|---|
National average: (300+400+500)/3 = 400. City averages: Dallas = (300+500)/2 = 400, Phoenix = 400. Since we need cities that exceed the national average, no cities qualify (both equal but don't exceed).
Constraints
-
1 ≤ listing_id ≤ 10^5 -
1 ≤ city.length ≤ 50 -
1 ≤ price ≤ 10^6 - City names contain only letters and spaces