GROUP BY
The clause that splits rows into groups sharing the same values, so aggregates return one result per group.
In more detail
Every selected column that is not inside an aggregate must be in the GROUP BY. Several columns form one combined group key, such as month and status. GROUP BY does not guarantee any order, so add ORDER BY.
Example
SELECT status, COUNT(*) AS orders FROM orders GROUP BY status;Related terms
- aggregate function — A function that turns many input rows into one value, such as COUNT, SUM, AVG, MIN or MAX.
- HAVING — The clause that filters groups after aggregates have been calculated, such as keeping customers with at least two orders.
- grain — What exactly one row of a result, table or report represents, such as one order, one order line or one customer per month.
- ORDER BY — The clause that sorts a query's answer by one or more columns, each ascending or descending.
Taught in
- Thirty Eight Percent More: Billing Models And Tags — CompTIA Cloud+ (CV0-004) Exam Prep
- The GROUP BY Clause — PostgreSQL, Indexing & Relational Data Modeling
- Grouping By Multiple Columns — PostgreSQL, Indexing & Relational Data Modeling
- The Difference Between WHERE And HAVING — PostgreSQL, Indexing & Relational Data Modeling