EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Median

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL Median

PostgreSQL Median

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • 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;

PostgreSQL Median-1.1

  • 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

PostgreSQL Median-1.2

  • 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;

PostgreSQL Median-1.3

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;

PostgreSQL Median-1.4

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;

PostgreSQL Median-1.5

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;

PostgreSQL Median-1.6

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 –

  1. PostgreSQL caching
  2. PostgreSQL CURRENT_TIMESTAMP()
  3. PostgreSQL Compare Strings
  4. PostgreSQL Text Search

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More