Beginner

596. JOIN with SUM

Learn how SUM can calculate totals from records combined through JOINs.

📘 Lesson 596 of 728 ⏱ 13 min 🎯 Intermediate

SUM adds numeric values from related records after tables are combined using a JOIN.

Total values such as sales, payments, balances, and transaction amounts are fundamental business metrics.

Calculate total order value for every customer.

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

Used in revenue analysis, financial reporting, sales dashboards, and transaction summaries.

  • SUM calculates numeric totals.
  • JOIN determines which records are included.
  • LEFT JOIN can preserve customers without orders.
  • COALESCE can convert NULL totals to zero.
  • GROUP BY controls the level of aggregation.
How is SUM used with JOIN?

SUM aggregates numeric values from related rows after the required tables have been joined.

Why use COALESCE around SUM?

It can convert a NULL result for entities with no matching rows into a meaningful zero.

Have you finished this lesson?

Introduction to SQL – All Lessons