Beginner

629. JOIN with Multiple Aggregates

Combine JOINs with multiple aggregate functions to create comprehensive business summaries.

📘 Lesson 629 of 728 ⏱ 14 min 🎯 Intermediate

Multiple aggregate expressions can summarize different aspects of related data within the same grouped JOIN query.

Business reports commonly require several KPIs together, such as count, total, average, minimum, and maximum.

Create a customer sales summary containing order count, total sales, average order, and highest order.

SQL
SELECT
    c.CustomerName,
    COUNT(o.OrderID) AS OrderCount,
    SUM(o.OrderAmount) AS TotalSales,
    AVG(o.OrderAmount) AS AverageOrder,
    MAX(o.OrderAmount) AS HighestOrder
FROM Customers c
LEFT JOIN Orders o
    ON c.CustomerID = o.CustomerID
GROUP BY
    c.CustomerName;

Used in management dashboards, sales MIS, financial reporting, and customer analytics.

  • Multiple aggregates can be used together.
  • All aggregates operate on the JOINed rows.
  • GROUP BY defines the summary level.
  • NULL behavior differs between aggregate functions.
  • Validate the JOIN grain before calculating KPIs.
Can multiple aggregate functions be used after a JOIN?

Yes. COUNT, SUM, AVG, MIN, and MAX can be used together in the same grouped query.

Why verify JOIN grain before calculating multiple KPIs?

Incorrect grain can multiply rows and make several aggregate values incorrect at the same time.

Have you finished this lesson?

Introduction to SQL – All Lessons