Beginner

454. JOIN with GROUP BY

Learn how GROUP BY summarizes information after tables have been joined.

📘 Lesson 454 of 728 ⏱ 12 min 🎯 Intermediate

GROUP BY divides joined rows into groups so aggregate functions can calculate separate results for each group.

Most analytical SQL queries combine JOIN with GROUP BY to create meaningful business summaries.

Calculate total sales for each customer after joining customers and orders.

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;

This pattern is common in revenue reporting, customer analysis, product analysis, and financial dashboards.

  • GROUP BY creates groups from joined rows.
  • Aggregate functions summarize each group.
  • Selected non-aggregated columns normally belong in GROUP BY.
  • JOIN determines the source relationships.
  • GROUP BY determines the reporting level.
Why is GROUP BY commonly used with JOIN?

It allows analysts to summarize related data at a business level such as customer, product, or department.

What happens if the JOIN relationship is incorrect?

Incorrect relationships can multiply rows and produce incorrect grouped totals.

Have you finished this lesson?

Introduction to SQL – All Lessons