Aggregate Functions
Aggregate functions operate on a set of values and return a single summarizing value. They are commonly used with GROUP BY clauses.
AVG – Average
Calculates the average (mean) of a numeric column.
Syntax: AVG ( expression )
SELECT AVG(Salary) AS AvgSalary FROM Employees;
Result
| AvgSalary |
|---|
| 75432.57 |
COUNT – Row Count
Returns the number of rows that match the specified criteria.
Syntax: COUNT ( [ * | expression ] )
SELECT Department, COUNT(*) AS EmployeesCount FROM Employees GROUP BY Department;
Result
| Department | EmployeesCount |
|---|---|
| Sales | 12 |
| HR | 5 |
MAX – Maximum Value
Returns the largest value of the selected column.
Syntax: MAX ( expression )
SELECT MAX(Price) AS HighestPrice FROM Products;
Result
| HighestPrice |
|---|
| 1299.99 |
MIN – Minimum Value
Returns the smallest value of the selected column.
Syntax: MIN ( expression )
SELECT MIN(Price) AS LowestPrice FROM Products;
Result
| LowestPrice |
|---|
| 4.99 |
SUM – Total Sum
Calculates the total sum of a numeric column.
Syntax: SUM ( expression )
SELECT SUM(Quantity) AS TotalUnits FROM OrderDetails;
Result
| TotalUnits |
|---|
| 5872 |
STDEV – Standard Deviation
Estimates the standard deviation of a set of values.
Syntax: STDEV ( expression )
SELECT STDEV(Salary) AS SalaryStdDev FROM Employees;
Result
| SalaryStdDev |
|---|
| 12458.33 |
VAR – Variance
Calculates the variance of a set of values.
Syntax: VAR ( expression )
SELECT VAR(Salary) AS SalaryVariance FROM Employees;
Result
| SalaryVariance |
|---|
| 155212890 |