Aggregate functions
Aggregate functions are a set of functions in SQL that allow you to perform calculations on multiple rows of data and return a single result. Some of the most commonly used aggregate functions in SQL are SUM
, AVG
, MIN
, and MAX
.
Here are examples of how to use these aggregate functions in SQL:
SUM
: This function is used to calculate the sum of a set of values.
SELECT SUM(amount) FROM orders;
In this example, the SUM
function is used to calculate the total amount of all orders in the orders
table.
AVG
: This function is used to calculate the average of a set of values.
SELECT AVG(price) FROM products;
In this example, the AVG
function is used to calculate the average price of all products in the products
table.
MIN
: This function is used to find the minimum value in a set of values.
SELECT MIN(date_ordered) FROM orders;
In this example, the MIN
function is used to find the earliest date an order was placed in the orders
table.
MAX
: This function is used to find the maximum value in a set of values.
SELECT MAX(date_ordered) FROM orders;
In this example, the MAX
function is used to find the latest date an order was placed in the orders
table.
These are just a few examples of how to use aggregate functions in SQL. There are many other aggregate functions available in SQL, such as COUNT
, GROUP BY
, HAVING
, etc., that can be used to perform more complex calculations on data stored in a relational database.
Leave a Comment