Airbnb-style Company ChallengeMediumVerified answerSQLite live

Average Rating per Listing

Return listing_id, city, and avg_rating rounded to 2 decimal places for each reviewed listing. Order by avg_rating descending, then listing_id.

  • Joins
  • Aggregation
  • Numeric functions
  • Sorting

Challenge brief

Understand the request

Quality — Host Performance The quality team monitors average ratings per listing to identify properties at risk of losing Superhost status.

Calculate the average guest rating for each listing.

Return

  • listing_id
  • city
  • avg_rating

Constraints

  • Include listings represented by review facts
  • Average every matching rating at listing grain
  • Order by average rating descending, then listing ID

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

bookings

  • booking_idINTEGER
  • listing_idINTEGER

listings

  • listing_idINTEGER
  • cityVARCHAR(50)

reviews

  • booking_idINTEGER
  • ratingINTEGER

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

A review reaches its listing through the related booking.

Hint 2

Aggregate ratings at listing and city grain.

Hint 3

Use listing ID to resolve equal averages.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT b.listing_id, l.city, ROUND(AVG(r.rating), 2) AS avg_rating FROM bookings b JOIN listings l ON b.listing_id = l.listing_id JOIN reviews r ON b.booking_id = r.booking_id GROUP BY b.listing_id, l.city ORDER BY avg_rating DESC, b.listing_id

Why this works

rating lives in reviews (linked via booking_id); city lives in listings (linked via listing_id). Both JOINs are needed to bring all three together. GROUP BY listing and ROUND(AVG) gives the quality metric per property.

Success check

Returns one stable row per reviewed listing with its two-decimal average rating

Expected result

Use this output to verify values, aliases, ordering, and row count.

listing_idcityavg_rating
3London5
6Paris5
5San Francisco4.5
1New York4
2Bangalore4
4Toronto4

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Interview Practice

Return to the complete interview preparation experience.