Loading section...

GROUP BY for categorizing data

Concepts: sqlGroupBy

Understanding GROUP BY How GROUP BY Works Consider this sales data: Watch how rows with the same region value are gathered into groups, then collapsed into a single summary row per group. The result has one row per region. Each row summarizes all the sales in that region. The original 6 rows have been collapsed into 3 summary rows. Rules and Patterns GROUP BY Golden Rule This rule exists because SQL needs to know how to handle columns that have multiple values per group. If you group by region, what should SQL show for sale_id? There are multiple sale_ids in each region. SQL cannot pick one arbitrarily, so you must tell it what to do: count them, sum them, take the max, etc. Multiple Column Grouping You can group by multiple columns to create more specific buckets. Each unique combination