100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
SQL Mastery
35 minbeginner

GROUP BY — Segmenting Your Data

A bare aggregate collapses an entire table into a single number, but usually you want one number per category: total runs per team, average score per venue, matches played per player. GROUP BY is the clause that splits rows into groups by shared column values, then runs the aggregate once for each group, producing a tidy summary table rather than a single grand total.

This is the leap from "what is the total" to "what is the total for each". Without GROUP BY you would run a separate filtered query for every team by hand, which is tedious and does not scale. GROUP BY lets the engine partition the data and compute every group's summary in one pass, returning a row per group in a single, efficient statement.

GROUP BY is the analytical backbone of SQL, the mechanism behind virtually every chart, pivot, and breakdown report you will ever build. Understanding precisely which columns may appear in the SELECT, and how grouping interacts with filtering and ordering, transforms aggregates from blunt single-number tools into a flexible engine for slicing data along any dimension you choose.

Analogy🏏Cricket
🏏 Think of it like cricket: Imagine the end of an IPL innings when the scorer announces the team's final total. Just as the scorer does not read out all one hundred and twenty individual deliveries but instead sums them into a single team score, SUM folds many row values into one number. Just as the commentator reports the highest individual score, the average partnership, and the number of wickets, MAX, AVG, and COUNT each summarise the innings differently. Just as a not-out batsman with no deliveries faced does not lower the team average, aggregates skip NULLs when computing. Just as the announcement is one figure no matter whether the innings had fifty deliveries or two hundred and fifty, an aggregate collapses any number of rows into a single result. This reveals why summaries answer the questions about the whole that individual deliveries cannot.
Lesson 8 of 35
0% complete