©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMariaDB

MariaDB MEDIAN SQL Function - Median value of a range of values

MEDIANSyntax:MEDIAN(Expression or column)
Return value:NUMERIC
Function type:Window function
Function Description

The MEDIAN() function returns the median value of a data window's range of values.


The MEDIAN() function is available in MariaDB from version 10.3.3.

The function does not exist in MySQL.

SQL Examples for the MEDIAN function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

DROP TABLE IF EXISTS `test_table`;

CREATE TABLE `test_table` (
  `id` int(11) NOT null AUTO_INCREMENT,
  `name` varchar(20) DEFAULT null,
  `type` enum('First','Second','Third','Last'DEFAULT null,
  `set` SET('First','Second','Third','Last'DEFAULT null,
  `num` int(11) NOT null DEFAULT 0,
  `price` decimal(10,2) NOT null DEFAULT 0.00,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

INSERT INTO `test_table` (`id`,`name`,`type`,`set`,`num`,`price`VALUES 
(1,'Art 1','First','Last',1,10.50),
(2,'Art 2','Second','First,Last',3,12.50),
(3,'Art 3','Second','Second,Last',2,15.50),
(4,'Art 4','Third','First,Second',5,25.50),
(5,'Art 5','Last','First,Last',2,5.50),
(6,'Art 6','First','First',12,1.50),
(7,'Art 7','Third','First,Third',20,1.75),
(8,'Art 8','Last','Second,Third',100,11.90);

select name,`type`,
    median(num) over (PARTITION BY `type`as n1, 
    median(price) over (PARTITION BY `type`as n2,
    median(num) over () as n3, 
    median(price) over () as n4 
from test_table;

name
type
n1
n2
n3
n4
test_table
varchar(20) LATIN1
test_table
enum LATIN1

double(18)

double(18)

double(18)

double(18)
Art 6First6.50000000006.00000000004.000000000011.2000000000
Art 1First6.50000000006.00000000004.000000000011.2000000000
Art 2Second2.500000000014.00000000004.000000000011.2000000000
Art 3Second2.500000000014.00000000004.000000000011.2000000000
Art 7Third12.500000000013.62500000004.000000000011.2000000000
Art 4Third12.500000000013.62500000004.000000000011.2000000000
Art 8Last51.00000000008.70000000004.000000000011.2000000000
Art 5Last51.00000000008.70000000004.000000000011.2000000000
The examples were created with the MyWAY SQL manager: Download

How to use the MEDIAN() function in MariaDB databases

In MariaDB the MEDIAN() function is used to calculate the median of a set of numeric values, where the Median is the mean in a sorted list of numbers that divides the data set into two equal halves. Column names, constants or expressions that result in numeric values ​​can be passed to the function as arguments. There should be at least one non-NULL value in the record. If the dataset contains an even number of values, the median is the average of the two means.

The MEDIAN() function can be used to find the mean or central tendency of a data set, for example for statistical analysis, where the median is a popular one a measure of central tendency in statistics, especially when it comes to skewed distributions or outliers in data. Further, the median can reveal the typical value when exploring datasets, providing a robust measure of centrality that is unaffected by extreme values ​​such as the mean. Also, medians are commonly used in reports and visualizations to summarize data. For example, in annual reports, median salary or median revenue can provide insight into the central trend of the data, providing a representative value for comparison or analysis. Comparing medians between different groups or categories may reveal variations or differences. By using the function with grouping and filtering, medians can be calculated for subsets of data and compared.
MAKETIMEMAXMD5MEDIANMICROSECONDMIDMIN

Further MySQL und MariaDB SQL Window functions

AVGAVG(Expression or column)
More about AVG Function

BIT_ANDBIT_AND(Expression or column)
More about BIT_AND Function

BIT_ORBIT_OR(Expression or column)
More about BIT_OR Function

BIT_XORBIT_XOR(Expression or column)
More about BIT_XOR Function

CUME_DISTCUME_DIST()

DENSE_RANKDENSE_RANK()

FIRST_VALUEFIRST_VALUE(Expression)

JSON_ARRAYAGGJSON_ARRAYAGG(Expression or column)

JSON_OBJECTAGGJSON_OBJECTAGG(Expression or column)

LAGLAG(Expression [, Rows] [, Default])

LAST_VALUELAST_VALUE(Expression [, Expression ...])
More about LAST_VALUE Function

LEADLEAD(Expression [, Rows] [, Default])

MAXMAX(Expression or column)
More about MAX Function

MINMIN(Expression or column)
More about MIN Function

NTH_VALUENTH_VALUE(Expression [, Row])

NTILENTILE(Number)

PERCENT_RANKPERCENT_RANK()

PERCENTILE_CONTPERCENTILE_CONT()

PERCENTILE_DISCPERCENTILE_DISC()

RANKRANK()

ROW_NUMBERROW_NUMBER()

STDSTD(Expression or column)
More about STD Function

STDDEVSTDDEV(Expression or column)
More about STDDEV Function

STDDEV_POPSTDDEV_POP(Expression or column)
More about STDDEV_POP Function

STDDEV_SAMPSTDDEV_SAMP(Expression or column)
More about STDDEV_SAMP Function

SUMSUM(Expression or column)
More about SUM Function

VAR_POPVAR_POP(Expression or column)
More about VAR_POP Function

VAR_SAMPVAR_SAMP(Expression or column)
More about VAR_SAMP Function

VARIANCEVARIANCE(Expression or column)
More about VARIANCE Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the MEDIAN SQL function: mariadb.com
Updated: 2023-09-24ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer