Vol. I · A Postgres HandbookTwelve chapters · · 756 queries

The Query & the Schema

A reading-room reference for writing PostgreSQL by hand — selects to window functions, with the schema in plain view.

FrontChaptersSchemaColophon
Chapters · GROUP BY & Aggregation · Query 013 / 024
AdvancedGROUP BY & Aggregation

Mode: most common salary value

The query

SQL
SELECT
  dept_id,
  MODE() WITHIN GROUP (ORDER BY salary) AS most_common_salary
FROM employees
GROUP BY dept_id;
Tested against PostgreSQL 16

Note

“MODE() returns the most frequent value. It's an ordered-set aggregate function.

Tables referenced

  • employees10 cols

← Previous

FILTER clause in aggregation

Next →

Pivot-style aggregation using CASE

The Query & the Schema

Set in Fraunces and Inter Tight. Printed on parchment, in oxblood and ink. No tracking, no accounts, no JavaScript on the answer pages beyond what your browser needs to copy a query.

Compiled for Postgres 16 · MMXXVI