SQL Aggregate Functions: SUM, AVG, MAX, MIN, COUNT

SQL Aggregate Functions: SUM, AVG, MAX, MIN, COUNT

SQL Aggregate Functions are used to perform mathematical operations on sets of values in a database table. They are used to summarize data and provide statistical information about the data. In this blog, we will discuss five commonly used SQL Aggregate Functions: SUM, AVG, MAX, MIN, and COUNT.

SUM Function

The SUM function is used to calculate the sum of all the values in a specified column of a database table. The syntax of the SUM function is as follows:

SELECT SUM(column_name)
FROM table_name;

For example, consider a table "sales" that has a "revenue" column. We can use the SUM function to calculate the total revenue of the company.

SELECT SUM(revenue)
FROM sales;

AVG Function

The AVG function is used to calculate the average value of a specified column of a database table. The syntax of the AVG function is as follows:

SELECT AVG(column_name)
FROM table_name;

For example, consider a table "employees" that has a "salary" column. We can use the AVG function to calculate the average salary of the employees.

SELECT AVG(salary)
FROM employees;

MAX Function

The MAX function is used to find the maximum value in a specified column of a database table. The syntax of the MAX function is as follows:

SELECT MAX(column_name)
FROM table_name;

For example, consider a table "products" that have a "price" column. We can use the MAX function to find the most expensive product.

SELECT MAX(price)
FROM products;

MIN Function

The MIN function is used to find the minimum value in a specified column of a database table. The syntax of the MIN function is as follows:

SELECT MIN(column_name)
FROM table_name;

For example, consider a table "products" that has a "price" column. We can use the MIN function to find the least expensive product.

SELECT MIN(price)
FROM products;

COUNT Function

The COUNT function is used to count the number of rows in a database table. The syntax of the COUNT function is as follows:

SELECT COUNT(*)
FROM table_name;

For example, consider a table "customers". We can use the COUNT function to count the total number of customers.

SELECT COUNT(*)
FROM customers;

Conclusion

SQL Aggregate Functions are a powerful tool for summarizing data in a database table. In this blog, we covered five commonly used Aggregate Functions: SUM, AVG, MAX, MIN, and COUNT. Understanding these functions and their syntax is essential for working with large datasets and generating statistical information about the data.