📝 What You’ll Learn
- What the
SUM()function does - Syntax for adding values in a column
- Practical examples with totals
- Using
SUM()withGROUP BYandWHERE
🔍 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
WHEREto filter before summing - Use with
GROUP BYfor 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()skipsNULLvalues- Works only on numeric columns
- Be sure to name your columns with
ASfor clarity in results
📘 Recap
- Use
SUM()to total numeric values in a column - Combine with
WHEREto sum conditionally - Use with
GROUP BYto break totals down by category