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

Aggregate Functions

Aggregate functions perform calculations on multiple rows and return a single summarized result.

What are Aggregate Functions?

Aggregate functions in SQL Server are used to perform calculations on a set of rows and return one summarized value.

They are commonly used with the GROUP BY clause when we need department-wise, category-wise, or other grouped results.

In simple words:

Aggregate functions take multiple records as input and produce a calculated result.

Common Aggregate Functions

Function Purpose Example
COUNT() Counts rows or values. COUNT(*)
SUM() Calculates the total. SUM(Salary)
AVG() Calculates the average. AVG(Salary)
MIN() Returns the smallest value. MIN(Salary)
MAX() Returns the largest value. MAX(Salary)

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

COUNT()

COUNT() is used to count rows or non-NULL values in a column.

SELECT COUNT(*) AS TotalEmployees
FROM Employees;

The above query returns the total number of employees.

Result:

TotalEmployees = 5

SUM()

SUM() calculates the total of numeric values.

SELECT SUM(Salary) AS TotalSalary
FROM Employees;

This query calculates the total salary of all employees.

Result:

TotalSalary = 230000

AVG()

AVG() calculates the average value of a numeric column.

SELECT AVG(Salary) AS AverageSalary
FROM Employees;

This query calculates the average salary of all employees.

Result:

AverageSalary = 46000

MIN()

MIN() returns the smallest value from a column.

SELECT MIN(Salary) AS MinimumSalary
FROM Employees;
Result:

MinimumSalary = 35000

MAX()

MAX() returns the largest value from a column.

SELECT MAX(Salary) AS MaximumSalary
FROM Employees;
Result:

MaximumSalary = 60000

Aggregate Functions with GROUP BY

Aggregate functions are frequently combined with GROUP BY to calculate results for each group.

SELECT
    Department,
    COUNT(*) AS EmployeeCount,
    SUM(Salary) AS TotalSalary,
    AVG(Salary) AS AverageSalary,
    MIN(Salary) AS MinimumSalary,
    MAX(Salary) AS MaximumSalary
FROM Employees
GROUP BY Department;

Important Points

  • Aggregate functions work on multiple rows.
  • They return a summarized result.
  • COUNT() counts records or values.
  • SUM() calculates totals.
  • AVG() calculates averages.
  • MIN() returns the smallest value.
  • MAX() returns the largest value.

Summary

Aggregate functions are used to perform calculations on multiple rows and return summarized information. The most commonly used aggregate functions in SQL Server are COUNT(), SUM(), AVG(), MIN() and MAX().