AnalystPath

Median-Priced Listing in Each Neighborhood

SQLHardSenior level~15 min

Problem

Table: `Listing`

```text
+--------------+---------+
| Column | Type |
+--------------+---------+
| listing_ref | int |
| neighborhood | varchar |
| price | int |
+--------------+---------+
listing_ref is the primary key.
```

For each neighborhood, return the row (or rows) holding the **median price**. When a neighborhood has an odd number of listings there is one median row; when it has an even number there are two. Sort prices ascending to find the middle, and break ties on equal prices by `listing_ref` ascending. Return the columns `listing_ref`, `neighborhood`, `price` in any order.

Tables

Example rows — the live problem includes the full dataset.

Listing
listing_refneighborhoodprice
1Harborside300
2Harborside500
3Harborside700

Expected output

Your answer should return 3 rows with the columns listing_ref, neighborhood, price.

Starter code (SQL)

SELECT *
FROM Listing;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions