Find Expensive Cities - Problem
Find Expensive Cities
You're working as a data analyst for a real estate company and need to identify luxury markets across the country. Given a database of property listings with their cities and prices, your task is to find all cities where the average home price exceeds the national average.
You have access to a
The
Your Goal: Write a SQL query that returns cities where the average home prices are above the national average, sorted by city name in ascending order.
This problem tests your ability to work with subqueries, aggregate functions, and data comparison across different groupings.
You're working as a data analyst for a real estate company and need to identify luxury markets across the country. Given a database of property listings with their cities and prices, your task is to find all cities where the average home price exceeds the national average.
You have access to a
Listings table with the following structure:| Column Name | Type |
|---|---|
| listing_id | int |
| city | varchar |
| price | int |
The
listing_id is unique for each property listing.Your Goal: Write a SQL query that returns cities where the average home prices are above the national average, sorted by city name in ascending order.
This problem tests your ability to work with subqueries, aggregate functions, and data comparison across different groupings.
Input & Output
example_1.sql โ Basic Example
$
Input:
Listings table:
| listing_id | city | price |
|------------|---------------|--------|
| 1 | New York | 500000 |
| 2 | New York | 600000 |
| 3 | Chicago | 300000 |
| 4 | Chicago | 350000 |
| 5 | San Francisco | 800000 |
| 6 | San Francisco | 900000 |
โบ
Output:
| city |
|---------------|
| New York |
| San Francisco |
๐ก Note:
National average = (500000+600000+300000+350000+800000+900000)/6 = 575000. New York average = 550000 (below national), San Francisco average = 850000 (above national), Chicago average = 325000 (below national). Only San Francisco exceeds the national average, but the expected output shows both cities, suggesting the national average calculation might be different or there's an error in the expected output.
example_2.sql โ Multiple Cities
$
Input:
Listings table:
| listing_id | city | price |
|------------|----------|--------|
| 1 | Miami | 400000 |
| 2 | Miami | 450000 |
| 3 | Austin | 300000 |
| 4 | Austin | 350000 |
| 5 | Seattle | 500000 |
| 6 | Seattle | 600000 |
| 7 | Denver | 250000 |
| 8 | Denver | 300000 |
โบ
Output:
| city |
|---------|
| Miami |
| Seattle |
๐ก Note:
National average = (400000+450000+300000+350000+500000+600000+250000+300000)/8 = 393750. Miami average = 425000 (above), Austin average = 325000 (below), Seattle average = 550000 (above), Denver average = 275000 (below). Miami and Seattle exceed the national average.
example_3.sql โ Edge Case: All Cities Above Average
$
Input:
Listings table:
| listing_id | city | price |
|------------|-------------|--------|
| 1 | Los Angeles | 700000 |
| 2 | Boston | 650000 |
| 3 | Portland | 200000 |
| 4 | Portland | 250000 |
โบ
Output:
| city |
|-------------|
| Boston |
| Los Angeles |
๐ก Note:
National average = (700000+650000+200000+250000)/4 = 450000. Los Angeles average = 700000 (above), Boston average = 650000 (above), Portland average = 225000 (below). Los Angeles and Boston exceed the national average.
Constraints
- 1 โค number of listings โค 105
- 1 โค price โค 107
- All prices are positive integers
- City names are non-empty strings with maximum length 50
- Each city has at least one listing
Visualization
Tap to expand
Understanding the Visualization
1
Calculate National Baseline
Find the overall average price across all listings to establish the national benchmark
2
Group Markets by City
Organize all listings by their respective cities and calculate each city's average price
3
Identify Premium Markets
Compare each city's average against the national average to find expensive markets
4
Present Results
Sort the premium cities alphabetically for a clean, organized output
Key Takeaway
๐ฏ Key Insight: Use SQL's GROUP BY to organize data by city, then HAVING clause to filter aggregated results - this is more efficient than multiple queries and leverages database optimization.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code