Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

SUM() Function

Learn how to calculate the total of numeric values using the SUM() aggregate function in SQL Server.

What is SUM()?

The SUM() function is an aggregate function in SQL Server used to calculate the total of numeric values from multiple rows.

It is commonly used for calculating total salary, total sales, total marks, total quantity, total price and other numerical values.

In simple words:

SUM() adds the numeric values together and returns their total.

SUM() Syntax

SELECT SUM(column_name)
FROM table_name;

We provide the numeric column inside the SUM() function.

Example Table

Consider the following Employees table:

EmployeeId EmployeeName Department Salary
1 Rahul IT 50000
2 Priya HR 40000
3 Amit IT 60000
4 Neha HR 45000
5 Rohit Sales 35000

Basic SUM() Example

To calculate the total salary of all employees:

SELECT SUM(Salary) AS TotalSalary
FROM Employees;

Calculation

50000 + 40000 + 60000 + 45000 + 35000
= 230000

Output

TotalSalary
230000

SUM() with WHERE

SUM() can be combined with WHERE when we want to calculate the total only for records satisfying a particular condition.

For example, calculate the total salary of employees whose salary is greater than 40000:

SELECT SUM(Salary) AS TotalSalary
FROM Employees
WHERE Salary > 40000;

Calculation

50000 + 60000 + 45000
= 155000

The result is 155000.

SUM() with GROUP BY

SUM() is commonly used with GROUP BY to calculate totals for each group.

For example, calculate the total salary for each department:

SELECT Department,
       SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Department;

Output

Department TotalSalary
HR 85000
IT 110000
Sales 35000

SUM() with HAVING

HAVING can be used to filter groups based on their calculated SUM() value.

For example, display only departments whose total salary is greater than 80000:

SELECT Department,
       SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Department
HAVING SUM(Salary) > 80000;

Output

Department TotalSalary
HR 85000
IT 110000

SUM() and NULL Values

SUM() ignores NULL values when calculating the total.

For example, if a Salary column contains:

50000
60000
NULL
40000

SUM() calculates the total using the available numeric values:

50000 + 60000 + 40000
= 150000

SUM(DISTINCT)

SUM(DISTINCT column) calculates the total using only unique values.

SELECT SUM(DISTINCT Salary) AS TotalSalary
FROM Employees;
Note:

DISTINCT removes duplicate values before calculating the total.

SUM() in Reports

SUM() is frequently used in reports to calculate business totals such as:

  • Total sales
  • Total salary
  • Total product quantity
  • Total order amount
  • Total revenue

Important Points

  • SUM() works with numeric values.
  • SUM() calculates the total of values.
  • SUM() ignores NULL values.
  • SUM() can be combined with WHERE.
  • SUM() can be combined with GROUP BY.
  • HAVING can filter grouped SUM() results.
  • SUM(DISTINCT column) calculates the total of unique values.

Summary

SUM() is an aggregate function used to calculate the total of numeric values. It is commonly used with WHERE, GROUP BY and HAVING clauses to perform detailed calculations and generate meaningful summary reports.