1# FOR TABLES WITH ODD NUMBER OF ROWS
2
3# For column1 in table1 with 'n' number of rows. Where 'n' is an odd number.
4# 1. Change all column1 and table1 to your column and table name.
5# 2. Calculate (n/2)+0.5, where n=number of rows, and set it as LIMIT for t1.
6
7SELECT *
8FROM (SELECT column1
9 FROM table1
10 ORDER BY column1
11 LIMIT (n/2)+0.5) AS t1
12ORDER BY column1 DESC
13LIMIT 1;
1# FOR TABLES WITH EVEN NUMBER OF ROWS
2
3# For column1 in table1 with 'n' number of rows. Where 'n' is an even number.
4# 1. Change all column1 and table1 to your column and table name.
5# 2. Calculate (n/2)+1, where n=number of rows, and set it as LIMIT for t1.
6
7SELECT AVG(t2.column1)
8FROM (SELECT *
9 FROM (SELECT column1
10 FROM table1
11 ORDER BY column1
12 LIMIT (n/2)+1) AS t1
13 ORDER BY column1 DESC
14 LIMIT 2) AS t2;
1SET @rowindex := -1;
2
3SELECT
4 AVG(g.grade)
5FROM
6 (SELECT @rowindex:=@rowindex + 1 AS rowindex,
7 grades.grade AS grade
8 FROM grades
9 ORDER BY grades.grade) AS g
10WHERE
11g.rowindex IN (FLOOR(@rowindex / 2) , CEIL(@rowindex / 2));
12