- COUNT: Counts the number of rows that match a specified condition.
- SUM: Calculates the sum of the values in a numeric column.
- AVG: Computes the average value of the values in a numeric column.
- MIN: Retrieves the minimum value from a column.
- MAX: Retrieves the maximum value from a column.
- GROUP_CONCAT: Concatenates values from multiple rows into a single string.
- STDEV: Calculates the standard deviation of a set of values.
- VAR: Calculates the variance of a set of values.
- FIRST: Returns the first value in a column.
- LAST: Returns the last value in a column.
COUNT: The COUNT
function is used to count the number of rows in a result set that meet a certain condition. It is often used to count the occurrences of a particular value or to retrieve the total number of rows in a table.
SELECT COUNT() FROM orders; -- Count all rows in the "orders" table SELECT COUNT() FROM customers WHERE age > 30; -- Count customers older than 30
SUM: The SUM
function calculates the sum of a numerical column’s values in a result set. It’s commonly used to find the total of a specific numeric attribute.
SELECT SUM(price) FROM products; -- Calculate the total price of all products
AVG: The AVG
function computes the average of a numerical column’s values in a result set. It’s used to find the average value of a specific numeric attribute.
SELECT AVG(age) FROM employees; -- Calculate the average age of employees
MIN: The MIN
function returns the minimum value from a column in a result set. It’s used to find the smallest value in a numeric or character column
SELECT MIN(salary) FROM staff; -- Find the lowest salary among staff members
MAX: The MAX
function retrieves the maximum value from a column in a result set. It’s used to find the largest value in a numeric or character column.
SELECT MAX(score) FROM test_results; -- Find the highest test score
These simple aggregate functions (COUNT
, SUM
, AVG
, MIN
, and MAX
) are fundamental tools in SQL for performing calculations and summarizing data in various ways. They are used extensively for generating insights and reports from database tables.