GROUP BY column
Data & Storage

Meaning

The GROUP BY clause groups rows that share the same values in specified columns into summary rows, enabling aggregate functions like COUNT, SUM, AVG, MAX, or MIN to compute statistics per group. It is typically used after a WHERE clause and before a HAVING clause in a SELECT statement.

Primary Function

Data aggregation

Communicative Purpose

Summarize data by categories

Pattern

SELECT agg_func(column) FROM table_name GROUP BY group_column;

Core Structure

SELECT ... FROM ... GROUP BY ... ;

Função primária

Data aggregation

Propósito comunicativo

Summarize data by categories

Situações de gatilho

Generating reports, calculating totals per category, preparing data for visualization

Contextos

Relational databases (SQL), data warehouses, BI tools, any SQL-like query language

Padrão

SELECT agg_func(column) FROM table_name GROUP BY group_column;

Estrutura central

SELECT ... FROM ... GROUP BY ... ;

Slots de substituição

agg_func: aggregate function name (e.g., COUNT, SUM, AVG), column: column to aggregate, table_name: name of the table, group_column: column to group by

Colocados típicos

  • HAVING clause
  • ORDER BY clause
  • JOIN clauses
  • aggregate functions

Substituições comuns

  • GROUP BY multiple columns (GROUP BY col1
  • col2)
  • GROUP BY expressions (GROUP BY UPPER(column))
  • using aliases in GROUP BY

Erros comuns

Forgetting to include non-aggregated columns in GROUP BY, causing syntax errors; using GROUP BY without any aggregate function leading to meaningless results; confusing GROUP BY with DISTINCT

Similar / contraste

DISTINCT (removes duplicate rows without aggregation); WINDOW functions (compute aggregates while preserving row detail)

Interferências

Coming from procedural languages: expecting GROUP BY to reorder rows like sorting; expecting it to filter rows like WHERE

Família do chunk

  • Aggregation
  • SQL clauses
  • data summarization

Nuance

GROUP BY operates after WHERE filtering and before HAVING filtering; NULL values are treated as a separate group; performance depends on indexes on the grouped columns

Efeito pragmático

Enables concise summarization of large datasets, reducing data volume for analysis

Dica de memória

Think 'group then aggregate'

Nota

GROUP BY does not guarantee row order; combine with ORDER BY for deterministic ordering

Upgrade path

Using GROUP BY with HAVING to filter groups, e.g., HAVING COUNT(*) > 1

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: SQL clausePrioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Medium-term

Log in to save chunks.