SQL Aggregate Functions

SQL aggregate functions are used to perform calculations on groups of rows or on an entire table in a database. These functions allow you to calculate the minimum, maximum, average, sum, and count of values in a column or a set of columns in a table.

The most commonly used SQL aggregate functions are:

SUM This function returns the total sum of a numeric column in a table.

SELECT SUM(column_name) 
FROM table_name;

AVG This function returns the average value of a numeric column in a table.

SELECT AVG(column_name) 
FROM table_name;

COUNT This function returns the number of rows in a table or the number of non-null values in a column.

SELECT COUNT(*) 
FROM table_name;

SELECT COUNT(column_name) 
FROM table_name;

MIN This function returns the smallest value in a column.

SELECT MIN(column_name) 
FROM table_name;

MAX This function returns the largest value in a column.

SELECT MAX(column_name) 
FROM table_name;

You can also use these aggregate functions in combination with the GROUP BY clause to group data by one or more columns and perform calculations on each group separately.

For example, the following query returns the total sales amount for each customer in the “orders” table:

SELECT customer_name, SUM(order_amount) 
FROM orders 
GROUP BY customer_name;

In conclusion, SQL aggregate functions are powerful tools for summarizing and analyzing data in a database. They allow you to perform complex calculations on large sets of data quickly and easily, making it easier to derive insights from your data.