SQL
Lab
Airbnb Airbnb Interview Question 07

Price
per Night
Analysis

Calculate average price per night per city.

Table Schema

Inspect
Table

interactive
ColumnType
listing_idinteger
cityvarchar
price_per_nightdecimal

Sample Data

Input
Output

Sample Input: listings
listing_idcityprice_per_night
101NYC100
102NYC150
103SF200
104SF250
105NYC120
Expected Output
cityavg_price
NYC123.33
SF225.00

SQL Editor

Run
Query

postgresql
Waiting for query

cityavg_price
NYC123.33
SF225.00

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT city, ROUND(AVG(price_per_night), 2) AS avg_price
FROM listings
GROUP BY city;

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving