Price per square foot by city

null handling

Price per square foot by city

Zillow SQL Interview Question

Zillow compares cities by the typical asking price per square foot. Some listings don't show a price yet, and they shouldn't count.

For each city, return the number of listings that have a price as priced_listings and the average price per square foot of those listings as avg_price_per_sqft, rounded to 2 decimal places. Sort by avg_price_per_sqft, highest first.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer

listingsTable16 rows

Column NameType
listing_idBIGINT
cityVARCHAR
bedroomsBIGINT
square_feetBIGINT
list_priceBIGINT
listed_dateDATE
statusVARCHAR

listingsExample Input

listing_idcitybedroomssquare_feetlist_pricelisted_datestatus
30001Seattle318509250002024-04-01active
30002Seattle21100NULL2024-04-02active
30003Denver424007800002024-04-02pending
30004Austin31700NULL2024-04-03active
30005Denver21050NULL2024-04-04active
30006Austin426006400002024-04-05sold
30007Seattle1720NULL2024-04-05active
30008Phoenix316004550002024-04-06active
30009Denver315005150002024-04-07active
30010Austin2980NULL2024-04-08pending
30011Phoenix422005990002024-04-08active
30012Seattle3190010100002024-04-09sold
30013Austin314503890002024-04-10active
30014Denver53100NULL2024-04-11active
30015Phoenix29003100002024-04-12active
30016Seattle42500NULL2024-04-12pending

Example Output

citypriced_listingsavg_price_per_sqft
Seattle2515.79
Denver2334.17
Phoenix3300.36
Austin2257.21

Explanation

Seattle has 5 listings, but only 2 show a price: 925,000 for 1,850 square feet, which is 500.00 per square foot, and 1,010,000 for 1,900 square feet, which is 531.58. Their average is 515.79.

The example above is a small slice of the data. Your query runs against the full tables.