/* MYSQL - obtain the median - MYSQL version 10.3.3 or greater      */
/* if even number of rows, you get the average of the 2 middle rows */

select MEDIAN(amount) OVER () AS median 
FROM payment
limit 1;



/* MYSQL - obtain the median - MYSQL version prior to 10.3.3     */
/* if even number of rows, does not average the 2 middle rows    */

select source.*, amount as median
from 
    (select @counter:=0 as counter) as counter      -- create a counter=0
    join
    (select @counter:=@counter+1 as rowNum, p.*     -- add 1 to counter, and select *
       from payment p                           
      order by amount) as source
where rowNum = floor((@counter+1)/2);