Beginner

497. JOIN with GROUP BY

Learn how GROUP BY can summarize data after combining related tables with JOIN.

📘 Lesson 497 of 728 ⏱ 12 min 🎯 Intermediate

GROUP BY groups rows based on one or more columns and allows aggregate functions to calculate summaries for each group.

Many business questions require summarized information across related tables rather than individual records.

Calculate total sales for every customer.

SQL
SELECT
    c.CustomerName,
    SUM(o.OrderAmount) AS TotalSales
FROM Customers c
INNER JOIN Orders o
    ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerName;

Used in sales summaries, customer analysis, employee reporting, branch performance, and financial reporting.

  • GROUP BY summarizes joined records.
  • Aggregate functions are commonly used with GROUP BY.
  • The grouping level determines the report level.
  • JOIN should establish the correct relationship first.
  • Incorrect relationships can produce incorrect aggregates.
Why use GROUP BY after a JOIN?

GROUP BY summarizes the related records at a chosen business level.

What can go wrong when GROUP BY follows an incorrect JOIN?

Incorrect row relationships can multiply records and produce inaccurate aggregate results.

Have you finished this lesson?

Introduction to SQL – All Lessons