▸_sqlgym

74. Median Home Price

HardRankingROW_NUMBERMedianRanking

SQLite has no MEDIAN() function, so you build it with ranking. Number the rows from both ends with ROW_NUMBER(): one ascending, one descending. The middle element(s) are exactly the rows where the two numbers are within 1 of each other:

  • odd count → one middle row (rn_asc = rn_desc),
  • even count → two middle rows (|rn_asc - rn_desc| = 1).

Averaging the selected rows gives the median in both cases.

The homes table has one row per home with a price.

Task: compute the median price. With 6 homes the median is the average of the 3rd and 4th cheapest. Round to 2 decimals.

Return a single column median_price (one row).

Tables
Loading schema…
Expected outputrow order doesn't matter
Computing expected output…

This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases 5test cases in all. A sloppy query that only fits the example won't pass.

SQL
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.