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
Syntax
mode() WITHIN GROUP (ORDER BY expression)
Parameters
| Parameter | Description |
|---|---|
ORDER BY |
The values to count. |
Return value
The most common value.
Examples
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.
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
| Version | Change |
|---|---|
| PostgreSQL 9.4 | Added |
Related
PERCENTILE_CONT()· Return the value at a percentile, interpolating between rows: percentile_cont(0.5) is the median.COUNT()· Count rows: count(*) counts every row, count(x) skips NULLs, and count(DISTINCT x) counts unique values. Returns bigint.- SQL Aggregate Functions · tutorial
- SQL GROUP BY · tutorial