📝 What You’ll Learn

  • What the SUM() function does
  • Syntax for adding values in a column
  • Practical examples with totals
  • Using SUM() with GROUP BY and WHERE

🔍 What Is SUM?

The SUM() function adds up the total of numeric values in a column.

It’s great for generating totals like sales amounts, hours worked, or any measurable quantity.

🧱 Basic Syntax

SELECT SUM(column_name) FROM table_name;
  • Returns the total of all non-null values in that column
  • Use with WHERE to filter before summing
  • Use with GROUP BY for grouped totals

🧪 Example: Total Sales

SELECT SUM(amount) AS total_sales
FROM orders;

🧪 Example: Sum with Filter

Total sales in January

SELECT SUM(amount) AS january_sales
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-01-31';

📊 Example with GROUP BY

Total sales by customer

SELECT customer_id, SUM(amount) AS customer_total
FROM orders
GROUP BY customer_id;

⚠️ Notes

  • SUM() skips NULL values
  • Works only on numeric columns
  • Be sure to name your columns with AS for clarity in results

📘 Recap

  • Use SUM() to total numeric values in a column
  • Combine with WHERE to sum conditionally
  • Use with GROUP BY to break totals down by category