PostgreSQL 18 reference · Aggregate Functions

PostgreSQL MODE() Function

Return the most frequent value in a group (the statistical mode). Ties go to the first value in sort order.

PostgreSQL 18 Since 9.4

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

mode() WITHIN GROUP (ORDER BY expression)

Parameters

ParameterDescription
ORDER BY The values to count.

Return value

The most common value.

Examples

Most common value
SELECT mode() WITHIN GROUP (ORDER BY status) AS most_common_status FROM orders;

Output

 most_common_status
--------------------
 delivered
(1 row)

Run this on your computer — see “Get Started” for setup.

Most common country
SELECT mode() WITHIN GROUP (ORDER BY country) AS top_country FROM customers;

Output

 top_country
-------------
 USA
(1 row)

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
PostgreSQL 9.4Added

Was this page helpful?