Beginner

71. GROUP BY with SUM

Learn how to calculate totals separately for each group using GROUP BY and SUM.

📘 Lesson 71 of 728 ⏱ 8 min 🎯 Beginner

GROUP BY can be combined with SUM to calculate the total of a numeric column for each group. SQL first creates groups based on the selected column and then calculates the sum separately for every group.

Business users often need totals by category, region, department, customer, or product instead of one overall total.

A sales manager wants to know the total sales generated by each region. GROUP BY Region with SUM(SalesAmount) can provide this summary.

SQL
SELECT
    Region,
    SUM(SalesAmount) AS TotalSales
FROM Sales
GROUP BY Region;

This pattern is commonly used for regional sales, department expenses, customer revenue, product sales, and financial reporting.

  • GROUP BY creates separate groups.
  • SUM calculates the total for each group.
  • The aggregated column should normally be numeric.
  • GROUP BY with SUM is common in business reports.
  • The result can be sorted using ORDER BY.
How do you calculate sales by region?

Use GROUP BY Region with SUM(SalesAmount).

What does SUM calculate with GROUP BY?

SUM calculates the total numeric value separately for each group.

Have you finished this lesson?

Introduction to SQL – All Lessons