Introduction to SQLSQL DatabaseSQL DatatypesSQL QueriesSQL DML StatementsSQL Constraints and IndexingSQL FunctionsStored Procedure and Triggers
SQL provides several aggregate functions to summarize data:
- COUNT(): Returns the number of rows.
- SUM(): Returns the total sum of a numeric column.
- AVG(): Returns the average of a numeric column.
- MIN(): Returns the minimum value.
- MAX(): Returns the maximum value.
1. COUNT()
The COUNT()
function returns the number of rows that match a specified condition. It can be used to count all rows or distinct values in a column.
Syntax:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SELECT COUNT(*) FROM employees;
2. SUM()
The SUM()
function calculates the total sum of a numeric column.
Syntax:
SELECT SUM(salary) FROM employees;
This query returns the total sum of all employee salaries in the employees
table.
3. AVG()
The AVG()
function calculates the average value of a numeric column.
Syntax:
SELECT AVG(salary) FROM employees;
This query returns the average salary of all employees in the employees
table.
4. MIN()
The MIN()
function returns the smallest value in a specified column.
Syntax:
SELECT MIN(salary) FROM employees;
This query returns the smallest salary in the employees
table.
5. MAX()
The MAX()
function returns the largest value in a specified column.
Syntax:
SELECT MAX(salary) FROM employees;