Cloud
mode
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 |
Examples
The following example uses a simplified version of the film table from the Pagila database, containing only the title, length and rating columns.
DROP TABLE IF EXISTS film;
CREATE TABLE film (
title text NOT NULL,
length int,
rating text
);
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 following query retrieves the most frequent ratings found in the film table:
SELECT MODE()
WITHIN GROUP (ORDER BY rating)
FROM film;
The query returns:
| mode |
|-------|
| NC-17 |
Was this helpful?