find the median in sql

Solutions on MaxInterview for find the median in sql by the best coders in the world

showing results for - "find the median in sql"
Orla
22 Nov 2016
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;
Isidora
12 Oct 2019
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;
Giovanni
04 Feb 2020
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
queries leading to this page
t sql median valuesql get median of a columnhow to query median in sqlmedian sql functionfind median of a column sqlselect median sqlsql find median of columnsql median valuefinding the median value in sqlselect median in sqlsql server medianget the median in sqlhow to find median of a column sqlto find median of data in sql querymedian of a column in sqlsql query to find medianmedian sql queryfinding median in sqlusing floor to find median in sqlsql find mediansql query to find median valuemedian code in sqlhow to find median of a column in sqlhow to find the median in sqlcalculate median in sqlmedian in column sqlmedian of data in sqlfind the median in sqlsql medianhow to find the mediansql median functionget median of row sqlsql find median valuemedian in sqlmedian select sqlsql get mediansql calculate mediancalculate median sqlhow to find median in sqlmedian sql server functionmedian 28 29 sqlfinding the median value from raw data in sqlcount median sqlfinding median using sqlmedian in sql serverhow to calculate median in sqlmedian sqlsql select the medianhow to get median by date sqlmedian function in sqlsql take median of operationfind median sqlsql select medianhow to get median of a column in sqlmedian in sql queryhow to calculate median value in sqlquerying the median sqlhow to get median of some field in sqlhow to find median in sql querycalculate median value in sqlget median of sql columnfind median in sqlsql median categorymedian of column sqlget median in sqlmedian formula in sqlselecting median in sqlfinding the median in sqlsql median of columnhow to get median sqlfind the median in sql