â The SUM() function is an aggregate function in SQL that calculates the total of numeric values in a column. It is widely used in reports, financial calculations, sales analysis, inventory management, and statistical summaries. The SUM()function works only with numeric data types and ignores NULL values.
đ What is SUM()?
The SUM() function adds together all numeric values in a specified column and returns the total. It can be used on an entire table or on filtered groups of records.
Information
đ Basic Syntax
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;đ Sample Table
Assume the following Students table:
| StudentID | Name | Department | Marks | FeesPaid |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 92 | 50000 |
| 102 | Bob | Mathematics | 85 | 45000 |
| 103 | Charlie | Physics | 78 | 40000 |
| 104 | David | Computer Science | 95 | 50000 |
| 105 | Eva | Mathematics | 88 | 47000 |
1ī¸âŖ Calculating the Total of a Column
Calculate the total fees paid by all students.
Total Fees Paid
SELECT SUM(FeesPaid) AS TotalFees
FROM Students;Result:
| TotalFees |
|---|
| 232000 |
2ī¸âŖ SUM() with WHERE
Use the WHERE clause to calculate totals for selected rows only.
Total Fees for Computer Science Students
SELECT SUM(FeesPaid) AS TotalCSFees
FROM Students
WHERE Department = 'Computer Science';3ī¸âŖ SUM() with DISTINCT
The DISTINCT keyword allows you to sum only unique values in a column.
SUM of Distinct Fees
SELECT SUM(DISTINCT FeesPaid) AS UniqueFeeTotal
FROM Students;Tip
đ SUM() with GROUP BY
The GROUP BY clause groups records, and SUM() calculates the total for each group.
Total Fees by Department
SELECT Department,
SUM(FeesPaid) AS TotalFees
FROM Students
GROUP BY Department;Sample Result:
| Department | TotalFees |
|---|---|
| Computer Science | 100000 |
| Mathematics | 92000 |
| Physics | 40000 |
đ SUM() with HAVING
The HAVING clause filters grouped results after totals have been calculated.
Departments with Total Fees Above 90000
SELECT Department,
SUM(FeesPaid) AS TotalFees
FROM Students
GROUP BY Department
HAVING SUM(FeesPaid) > 90000;đ SUM() with JOIN
The SUM() function is frequently used with joins to calculate totals across related tables.
Total Course Fees per Student
SELECT s.Name,
SUM(c.CourseFee) AS TotalCourseFee
FROM Students s
JOIN Courses c
ON s.StudentID = c.StudentID
GROUP BY s.Name;đ§Ž SUM() with Expressions
You can use arithmetic expressions inside the SUM() function.
Total Marks with Bonus
SELECT SUM(Marks + 5) AS TotalBonusMarks
FROM Students;â ī¸ SUM() and NULL Values
The SUM() function automatically ignores NULL values. Only valid numeric values are included in the total.
| Column Values | SUM() Result |
|---|---|
| 10, 20, NULL, 30 | 60 |
Important
đ Common Uses of SUM()
| Purpose | Example |
|---|---|
| Total Sales | SUM(SalesAmount) |
| Total Salary | SUM(Salary) |
| Total Marks | SUM(Marks) |
| Total Fees | SUM(FeesPaid) |
| Department-wise Totals | SUM(...) GROUP BY Department |
đŧ Real-World Example
A university administrator wants to calculate the total fees collected from each department.
Department-wise Fee Collection
SELECT Department,
SUM(FeesPaid) AS TotalCollection
FROM Students
GROUP BY Department
ORDER BY TotalCollection DESC;This query generates a summary report showing the total fee collection for every department.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ SUM() calculates the total of numeric values.
- đ It ignores NULL values.
- đ SUM(DISTINCT Column) adds only unique values.
- đ It is commonly used with WHERE, GROUP BY, and HAVING.
- đ SUM() can be combined with expressions and joins.
- đ It is widely used for financial reports, analytics, and business intelligence.