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 · Basic SELECT · Query 009 / 136
EasyBasic SELECT

Count employees in each department (just count)

The query

SQL
SELECT
  dept_id,
  COUNT(*) AS employee_count
FROM employees
GROUP BY dept_id;
Tested against PostgreSQL 16

Note

“GROUP BY aggregates rows sharing the same value into summary rows.

Tables referenced

  • employees10 cols

← Previous

Select all departments and their locations

Next →

Select first 5 products by price descending

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