# The Mode Ordered-Set Aggregate Function

DevFeed: [The Mode Ordered-Set Aggregate Function](<https://devfeed.tech/articles/the-mode-ordered-set-aggregate-function-34570.md>)

Original publisher: [Read original article](<https://tapoueh.org/blog/2017/11/the-mode-ordered-set-aggregate-function/>)

Author: Dimitri Fontaine PostgreSQL Major Contributor; Author

Published: 2017-11-13T17:15:51Z

Content type: tutorial

Language: en

Sources: [Dimitri Fontaine](<https://devfeed.tech/sources/dimitri-fontaine.md>)

Topics: [PostgreSQL](<https://devfeed.tech/topics/postgresql.md>), [SQL](<https://devfeed.tech/topics/sql.md>), [Databases](<https://devfeed.tech/topics/databases.md>), [data](<https://devfeed.tech/topics/data.md>)

Tags: [aggregate](<https://devfeed.tech/tags/aggregate.md>), [aggregate-functions](<https://devfeed.tech/tags/aggregate-functions.md>), [analytics](<https://devfeed.tech/tags/analytics.md>), [cte](<https://devfeed.tech/tags/cte.md>), [postgresql](<https://devfeed.tech/tags/postgresql.md>), [sort](<https://devfeed.tech/tags/sort.md>), [sql](<https://devfeed.tech/tags/sql.md>), [statistics](<https://devfeed.tech/tags/statistics.md>)

## AI overview

A tutorial explains how PostgreSQL's ordered-set aggregate function mode() WITHIN GROUP selects the most frequent genre for albums in the Chinook sample database, including ties and the use of a CTE.

## Source excerpt

In our article Exploring a Data Set in SQL we discovered a data set related to music: the Chinook sample database. Our discovery led us to find albums containing tracks of multiple genres, and for the analytics we were then pursuing, we wanted to clean the data set and assign a single genre per album. We did that in SQL of course, and didn't actually edit the data. Finding the most frequent input value in a group is a job for the mode() WITHIN GROUP (ORDER BY sort_expression) Ordered-Set Aggregate Function, as documented in the PostgreSQL page about Aggregate Functions.