Definition of PostgreSQL Median
PostgreSQL median is defined as find the median value from the column on which we have used median, there is no such built-in function available like median we can implement the same by creating user defined function. Also, we can find the median of the column by using the percentile_disc (0.5) while using percentile 0.5 it will return the median from the PostgreSQL column table. We are using the order by the operator with a column name to find the median value from the table column.
Syntax and Parameter
Below is the syntax of the median in PostgreSQL.
- Find the median using percentile_disc (0.5)
Select percentile_disc(0.5) (Percentile disc with 0.5 used to find median from the table column.)within group (order by name_of_table.name_of_table);
- Find median by creating function and aggregate
SELECT median (We have calling aggregate function with specifying column name) (name_of_column) AS (alias of column name) FROM name_of_table;
Parameter:
PostgreSQL median parameter description is as follows.
- Select: This operation is used to select a specified column from the table from which we have finding median value.
- Name of column: This is defined as a column name from which we have retrieving the median value.
- Name of table: This is defined as the name of the table from which column name was are finding median value in PostgreSQL.
- Percentile disc (0.5): We have using percentile_disc (0.5) to find the median from the column in PostgreSQL. There is no function available like median so we have using percentile_disc (0.5) instead of the median.
- Median: This is defined as find the median from the table column. We have to find the median value on the basis of the column which was we have used in the median.
- Alias of column name: This is defined as we have using other name of column which was displaying in output.
- Within group: This is defined as a clause that was used to find the median from the table column in PostgreSQL. This clause is used to calculate the percentile value from the specified group.
How does Median Work in PostgreSQL?
- Below is the working of the median in PostgreSQL.
- Median is defined as the median value from the series which was used with the query. We can also use column name instead of series in PostgreSQL.
- If suppose we have used series value as even number then the median will return the mean of two values as the median in PostgreSQL.
- The median value in PostgreSQL is more representative as compared to the mean value in PostgreSQL.
- If suppose we have used series value as an odd number then the median will return the middle value as the median in PostgreSQL.
- The below example shows that if we have used a series value as an odd number then the median will return the middle value as the median.
- In the below example, the stud1 table contains the 11 records i.e. odd number so the median will return as the 6th number of value from the table.
- We have used the id table to find the median from the column.
select * from stud1;
select percentile_disc(0.5) within group (order by stud1.id) from stud1;
- We are creating user defined median function in PostgreSQL, after creating the same we have using it later.
- While creating function it will actually shows the value of median in PostgreSQL of an even number of series.
- We can also calculate the median in PostgreSQL by using the 50th percentile value because there is no built-in function available in PostgreSQL to calculate the median.
- We are using a within-group clause and order by clause with percentile (0.5). The main use of order by clause in percentile is to sort the value as per order before calculating the median in PostgreSQL.
Examples of PostgreSQL Median
- Below is the example of the median in PostgreSQL. We are using the function as median_function1 and aggregate as median_function2 to describe an example of the median in PostgreSQL.
- Below is the structure of aggregate and function.
\sf median_function1
\da+ median_function2
- Also, we are using the median_function table to describe an example of the median in PostgreSQL. Below is the median_function table and data.
select * from median_function;
\d+ median_function;
Find the median by creating function and aggregate
- The below example shows that find median by creating function and aggregate. We have already created a function name as median_function1 and aggregate name as median_function2.
- We are using id column with aggregate as median_function2 to find the median from the median_function table.
SELECT median_function2(id) AS median_value_id FROM median_function;
Find median of even number column values by creating function and aggregate
- The below example shows that find median of even number column values by creating function and aggregate.
- We are using function name as median_function1 and aggregate name as median_function2.
delete from median_function where id = 12;
SELECT median_function2(id) AS median_value_id FROM median_function;
select * from median_function;
Find median by using percentile_disc (0.5)
- The below example shows that find the median by using percentile_disc (0.5).
- We are using the id column to find the median from the median_function table.
insert into median_function values (11, 111, 'ABC', 'Delhi');
select percentile_disc(0.5) within group (order by median_function.id) from median_function;
select * from median_function;
Recommended Articles
This is a guide to PostgreSQL Median. Here we also discuss the definition and how does median work in PostgreSQL? along with different examples and its code implementation. You may also have a look at the following articles to learn more –