Aggregate functions in SQL are very useful when we want to summarize data at specific times. These functions can help us easily calculate numbers and figures that we have in a table. Now let's discuss what you have.
Functions like SUM
and AVG
can calculate the sum and average of numeric columns. For example, when we want to calculate the total sales of a seller, we use the SUM
function. This type of calculation can sometimes take a long time.
If you are looking for the minimum or maximum value in a column, the MIN
and MAX
functions are ideal. These can easily tell you the smallest or largest value within a given list of numbers.
The COUNT
function also serves a practical purpose when you want to obtain the number of records or entries in a table or the number of a particular condition. For example, when you want to count how many people are working in a sales department.
SELECT SUM(sales) FROM orders;
SELECT AVG(price) FROM products;
SELECT MIN(age) FROM users;
SELECT MAX(salary) FROM employees;
SELECT COUNT(*) FROM customers;
Line-by-Line Explanation
SELECT SUM(sales) FROM orders;
This line calculates the total sales from the orders table.
SELECT AVG(price) FROM products;
This line calculates the average price from the products table.
SELECT MIN(age) FROM users;
This line calculates the minimum age from the users table.
SELECT MAX(salary) FROM employees;
This line calculates the highest salary from the employees table.
SELECT COUNT(*) FROM customers;
This line calculates the total number of customers from the customers table.