# 571. Find Median Given Frequency of Numbers

## Problem

Table: Numbers

```
+-------------+------+
| Column Name | Type |
+-------------+------+
| num | int |
| frequency | int |
+-------------+------+
num is the primary key for this table.
Each row of this table shows the frequency of a number in the database.
```

The median is the value separating the higher half from the lower half of a data sample.

Write an SQL query to report the median of all the numbers in the database after decompressing the Numbers table. Round the median to one decimal point.

The query result format is in the following example.

Example 1:

```
Input:
Numbers table:
+-----+-----------+
| num | frequency |
+-----+-----------+
| 0 | 7 |
| 1 | 1 |
| 2 | 3 |
| 3 | 1 |
+-----+-----------+
Output:
+--------+
| median |
+--------+
| 0.0 |
+--------+
Explanation:
If we decompress the Numbers table, we will get [0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 3], so the median is (0 + 0) / 2 = 0.
```

## Code

```
# Write your MySQL query statement below
select avg(n.num) median
from Numbers n
where n.frequency >= abs((select sum(frequency) from Numbers where num<=n.num) -
(select sum(frequency) from Numbers where num>=n.num))
```

按 <- 键看上一题！
570. Managers with at Least 5 Direct Reports

按 -> 键看下一题！
572. Subtree of Another Tree