Overview

MODE() is an ordered-set aggregate function that returns the most frequently occurring value (the mode) from a set of values.

Syntax

MODE() WITHIN GROUP (ORDER BY order_list)
Null values are ignored during the calculation. If null is the most frequent value, the function will return the second most common value.

Parameters

  • (): this function takes no parameters, but empty parentheses is required

Example

For the needs of this section we will use a simplified version of the film table from the Pagila database, that will contain only the title, length and rating columns.

DROP TABLE IF EXISTS film;
CREATE TABLE film (
  title text NOT NULL,
  rating text,
  length int
);
INSERT INTO film(title, length, rating) VALUES
  ('ATTRACTION NEWTON', 83, 'PG-13'),
  ('CHRISTMAS MOONSHINE', 150, 'NC-17'),
  ('DANGEROUS UPTOWN', 121, 'PG'),
  ('KILL BROTHERHOOD', 54, 'G'),
  ('HALLOWEEN NUTS', 47, 'PG-13'),
  ('HOURS RAGE', 122, 'NC-17'),
  ('PIANIST OUTFIELD', 136, 'NC-17'),
  ('PICKUP DRIVING', 77, 'G'),
  ('INDEPENDENCE HOTEL', 157, 'NC-17'),
  ('PRIVATE DROP', 106, 'PG'),
  ('SAINTS BRIDE', 125, 'G'),
  ('FOREVER CANDIDATE', 131, 'NC-17'),
  ('MILLION ACE', 142, 'PG-13'),
  ('SLEEPY JAPANESE', 137, 'PG'),
  ('WRATH MILE', 176, 'NC-17'),
  ('YOUTH KICK', 179, 'NC-17'),
  ('CLOCKWORK PARADISE', 143, 'PG-13');

The query below retrieves the most frequent ratings found in the film table:

SELECT MODE()
  WITHIN GROUP (ORDER BY rating)
FROM film; 

By executing the code above we will get the following output:

| mode  |
|-------|
| NC-17 |