HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How do I find the median value of a column in MySQL?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
thehowcolumnvaluemysqlfindmedian

Problem

I can only imagine doing this with two database queries. The first finds the number of rows in the database, and the second selects with an ORDER BY on the column I am interested in and LIMIT X, 1 where X is half the number of rows.

Is there a simple way to do this with only one query?

Right now I am using averages in my calculations, but I think the mean would be better; there is no upper bound to the values and they are bounded from below by 0.

EDIT: yes, I meant to say 'median' but was having some brain error & searched for 'mean'. I have now found the answer over at stackoverflow

Solution

There is quite a bit of discussion here on calculating median values from a MySQL table. Just search the page for 'median'.

As an aside, it strikes me as remiss that there is no built-in function to do this. Median is often more descriptive of central tendency than mean. Access/VBA has the same hole in its function list.

Context

StackExchange Database Administrators Q#2519, answer score: 6

Revisions (0)

No revisions yet.