GROUP BY (EN)

Concept

SQL clause for grouping query results based on column values

GROUP BY in SQL

GROUP BY is an SQL clause used to group query results based on the values of one or more columns. This clause is typically used in conjunction with aggregate functions such as COUNT, SUM, AVG, MIN, or MAX to calculate an aggregated value for each group. The GROUP BY clause follows the WHERE clause in a SQL statement and precedes the HAVING clause, which is used to filter groups based on aggregated values. The correct application of GROUP BY is crucial for creating summary reports and data analysis in relational databases.

Data flow with GROUP BY

flowchart TD     A[Raw data] --> B[WHERE filter]     B --> C[GROUP BY grouping]     C --> D[Aggregate functions]     D --> E[HAVING filter on groups]     E --> F[Result] 

In context

  • Typically used together with aggregate functions (COUNT, SUM, AVG, MIN, MAX)
  • Related to: HAVING, ORDER BY, DISTINCT, ROLLUP, CUBE
  • Example use: Calculate sales per product category
Quelle: AI Generated