What Is GROUP BY in SQL?

SQL’s GROUP BY clause partitions result-set rows into groups so you can run aggregate functions (COUNT, SUM, AVG, MIN, MAX) on each group. Without GROUP BY, aggregates cover the entire table. Use it whenever you need per-category totals, averages, or other summarized metrics.

1
minute read
Learning
June 10, 2025
Sign up for the latest notes from our team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.
GROUP BY divides rows into groups and lets aggregate functions—COUNT, SUM, AVG, MIN, MAX—return one result per group instead of one for the whole table.

Table of Contents

What Is the SQL GROUP BY Clause?

GROUP BY breaks a result set into logical buckets so aggregate functions return one row per bucket. Without it, COUNT(*), SUM(), or AVG() work on the entire table.

Use GROUP BY after the FROM/WHERE phase and before HAVING/ORDER BY. The database scans rows, forms groups based on the listed columns, then calculates aggregates for each group.

When Should I Use GROUP BY?

Use GROUP BY when you need totals, averages, or other metrics per category—sales per month, logins per user, or bugs per severity. It condenses many detail rows into meaningful summaries.

What Is the Basic GROUP BY Syntax?

Place GROUP BY after WHERE: SELECT col1, agg(col2) FROM table WHERE ... GROUP BY col1; All non-aggregated selected columns must appear in the GROUP BY list.

How Do I Group By Multiple Columns?

Add more columns to the clause: GROUP BY region, product. The engine forms a unique group for every region-product combination, then calculates aggregates for each.

Can I Use Aggregate Functions Without GROUP BY?

Yes. Omitting GROUP BY returns a single aggregated row for the entire filtered data set. Include GROUP BY to get one aggregated row per group.

How Do I Filter Aggregated Rows?

Use HAVING, not WHERE. HAVING runs after GROUP BY, so it can reference aggregate results. Example: HAVING SUM(amount) > 1000.

How Do I Use GROUP BY with JOINs?

Join tables first, then group the combined rows. Ensure that selected non-aggregated columns come from the joined tables and are listed in GROUP BY.

How Does GROUP BY Treat NULL Values?

Rows with NULL in a grouping column form a single NULL group. Aggregates ignore NULLs except COUNT(*).

What Are Best Practices for GROUP BY?

Select only needed columns, index grouping columns for speed, move calculations into aggregates, and filter early with WHERE to reduce grouped rows.

How Can I Improve GROUP BY Performance?

Use covering indexes on grouping and filtering columns, avoid grouping large text fields, and materialize intermediate results with CTEs or temp tables when sets are huge.

How Do I Add Subtotals with ROLLUP?

Many SQL engines support GROUP BY ROLLUP(col1, col2) to add subtotal rows per higher-level grouping. This produces hierarchical summaries in one query.

What Is GROUP BY CUBE?

CUBE generates all possible subtotal combinations for the listed columns. It’s useful for multidimensional analysis but can create many rows, so use judiciously.

Key Takeaways

GROUP BY forms groups, aggregates calculate per-group metrics, HAVING filters them, and indexes speed things up. Mastering GROUP BY unlocks quick summaries and business insights.

Frequently Asked Questions (FAQs)

Does GROUP BY change row order?

No. Use ORDER BY after GROUP BY if you need a specific order.

Can I GROUP BY an expression?

Yes. Repeat the exact expression in SELECT and GROUP BY, or use a column alias in some engines.

Is GROUP BY required with DISTINCT?

No. DISTINCT removes duplicates on selected columns without aggregates. Use GROUP BY only when you need aggregates per group.

How do I group by date ignoring time?

Truncate or cast the timestamp to date in both SELECT and GROUP BY, e.g., DATE(purchased_at).

Start Vibe Querying with Galaxy Today!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Check out our other posts!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo