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 Listings table with the following structure:

Column NameType
listing_idint
cityvarchar
priceint

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
City Average Prices vs National AverageNational Avg: $450KNew York$550KSan Francisco$850KChicago$325KDallas$225KExpensive Cities (Above National Average)Affordable Cities (Below National Average)Result: New York, San Francisco
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.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
26.8K Views
Medium-High Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen