## Medians in SQL

### Want to make data analysis fast for everyone?

Join Us!Finding the ** min**,

**, and**

`max`

**price of an order in your purchases table is easy. But what about the**

`avg`

**price? Medians are much more difficult for the database to compute, so there usually isn’t a built-in function like the following:**

`median`

### Why is Median Hard?

For the standard aggregate functions (** min**,

**,**

`max`

**, etc.) it’s possible to get the result in a single pass over the data, storing the current**

`count`

**/**

`min`

**/**

`max`

**along the way. If the column is indexed, the minimum or maximum value can be found without looking at any rows!**

`count`

** Median** cannot work like that because it’s algorithmically much more complicated. The median is the middle value. The data needs to be sorted so that the middle record can be found.

### Finding the Median with Different Databases

Since median is harder than ** min**/

**/**

`max`

**and not a standard aggregate function, we’ll need to do a little more work to calculate it. Here’s how on several different databases:**

`count`

#### Median on Redshift

The Redshift team recently released a `median`

window function, making it one of the easiest syntaxes to find the median with:

Note the ** limit 1**: Since

**is a window function and not an aggregate function, it’ll return one value for each row in the table.**

`median`

#### Median on Postgres

If you like defining your own functions in Postgres, the Postgres Wiki has a definition for `median`

. We’ll do it in SQL and get Postgres to help us find the middle value by numbering all the rows with the ** row_number()** window function.

First, a CTE to sort and number all of the rows, with a ** count** that’ll help later on:

Then we find the middle one or two rows and average their values:

The ** where** clause ensures that we’ll get the two middle values if there is an even number of values, and the single middle number if there is an odd number of values because

**is inclusive of its bounds.**

`between`

#### Median on MySQL

MySQL might not have window functions, but it does have variables, and we’ll use them to achieve the same result.

First we’ll set two variables, one for the row count and one to act as the ** row_id** from before:

And just like before, we average the middle one or two values:

The ** @row_id := @row_id + 1** syntax simply increments the

**counter for each row. Unlike Postgres we don’t need to build up a temporary result set of rows with**

`@row_id`

**because variables let us compute the**

`row_id`

**on the fly.**

`row_id`

### Bonus Round: Mode

No post with ** avg** and

**is complete without**

`median`

**! The**

`mode`

**(defined as the most frequent value) of a column can be found with a simple group and count:**

`mode`

This query gives us the ** price** with the greatest

**in the table, also known as the mode.**

`count()`

And now you know how to compute median in SQL for Redshift, Postgres and MySQL databases!