GROUP BY

SQL · Grouping Rows

Theory

GROUP BY collapses rows that share the same value(s) into one row per group, so aggregate functions can summarise each group: COUNT(*) counts rows, SUM and AVG add and average, MIN and MAX find extremes. GROUP BY dept with COUNT(*) and SUM(salary) turns five employee rows into one summary row per department.

Every column in SELECT must either be in GROUP BY or be wrapped in an aggregate; otherwise the engine cannot know which value to show (MySQL in some modes and SQLite allow it but return an arbitrary value, which is a common bug). Grouping on several columns makes one group per unique combination. NULLs in a grouping column form their own single group. GROUP BY runs after WHERE, so WHERE filters rows first, and HAVING then filters groups.

Why we use it

Raw rows answer "what happened"; GROUP BY answers "how much per category": total sales per city, headcount per department, average order value per month. It is the core of almost every report.

When to use it

Use GROUP BY whenever you need one result row per category with totals, counts or averages. If you need the aggregate next to each detail row instead of collapsing rows, use window functions such as SUM() OVER (PARTITION BY ...).

How it works

  1. Rows arrive after WHERE
  2. Read the grouping column value(s) of each row
  3. Does a group with this value exist? Yes: Add the row to that group. No: Start a new group.
  4. Compute aggregates (COUNT, SUM, AVG...) per group
  5. Return one row per group

Syntax

SELECT column, AGG(column)
FROM table
GROUP BY column;

Example

SELECT department, COUNT(*) FROM employees GROUP BY department;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic