In MySQL/MariaDB, you can try this code:
SELECT AVG(dd.val) as median_val
FROM (
SELECT d.val, @rownum:=@rownum+1 as `row_number`, @total_rows:=@rownum
FROM data d, (SELECT @rownum:=0) r
WHERE d.val is NOT NULL
-- put some where clause here
ORDER BY d.val
) as dd
WHERE dd.row_number IN ( FLOOR((@total_rows+1)/2), FLOOR((@total_rows+2)/2) );
According to Steve Cohen, after the first pass, @rownum will contain the total number of rows.
This will be useful to determine the median, so no second pass or join is needed.
Also, dd.row_number IN(...) and AVG(dd.val) is used to produce the median when there is an even number of records.
Reasoning:
SELECT FLOOR((3+1)/2),FLOOR((3+2)/2); -- when total_rows is 3, avg rows 2 and 2
SELECT FLOOR((4+1)/2),FLOOR((4+2)/2); -- when total_rows is 4, avg rows 2 and 3
Note: MariaDB 10.3.3+ contains the MEDIAN function.