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

AVG() Function

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

What is AVG()?

The AVG() function is an aggregate function in SQL Server used to calculate the average value of a numeric column.

It is commonly used to calculate average salary, average marks, average price, average rating and other numerical values.

In simple words:

AVG() adds the values and calculates their average.

AVG() Syntax

SELECT AVG(column_name)
FROM table_name;

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 AVG() Example

To calculate the average salary of all employees:

SELECT AVG(Salary) AS AverageSalary
FROM Employees;

Calculation

(50000 + 40000 + 60000 + 45000 + 35000) / 5
= 46000

Output

AverageSalary
46000

AVG() with WHERE

AVG() can be combined with WHERE to calculate the average only for records that satisfy a condition.

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

SELECT AVG(Salary) AS AverageSalary
FROM Employees
WHERE Salary > 40000;

Calculation

(50000 + 60000 + 45000) / 3
= 51666.67

AVG() with GROUP BY

AVG() can be combined with GROUP BY to calculate the average value for each group.

SELECT Department,
       AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department;

Output

Department AverageSalary
HR 42500
IT 55000
Sales 35000

AVG() with HAVING

HAVING can be used to filter groups based on their average value.

For example, display departments whose average salary is greater than 40000:

SELECT Department,
       AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > 40000;

Output

Department AverageSalary
HR 42500
IT 55000

AVG() and NULL Values

AVG() ignores NULL values while calculating the average.

For example:

50000
60000
NULL
40000

SQL Server calculates the average using the three available numeric values:

(50000 + 60000 + 40000) / 3
= 50000

AVG(DISTINCT)

AVG(DISTINCT column) calculates the average using only unique values.

SELECT AVG(DISTINCT Salary) AS AverageSalary
FROM Employees;
Note:

DISTINCT removes duplicate values before calculating the average.

AVG() with ROUND()

We can use ROUND() when we want to control the number of decimal places in the average result.

SELECT ROUND(AVG(Salary), 2) AS AverageSalary
FROM Employees;

Real-World Uses of AVG()

  • Calculate average employee salary.
  • Calculate average student marks.
  • Calculate average product price.
  • Calculate average customer rating.
  • Calculate average monthly sales.

Important Points

  • AVG() is an aggregate function.
  • AVG() works with numeric values.
  • AVG() ignores NULL values.
  • AVG() can be used with WHERE.
  • AVG() can be used with GROUP BY.
  • HAVING can filter grouped AVG() results.
  • AVG(DISTINCT column) calculates the average of unique values.

Summary

AVG() is used to calculate the average of numeric values in SQL Server. It can be combined with WHERE, GROUP BY and HAVING to calculate averages for specific records or groups.