In SQL, aggregate functions are used to perform calculations on multiple rows of data to return a single result. These functions are typically used in conjunction with the GROUP BY clause to group the rows before performing the calculation. The most commonly used aggregate functions in SQL include COUNT, SUM, AVG, MIN, and MAX.
The COUNT function is used to count the number of rows that match a specified condition. It is often used to determine the number of records in a table or the number of records that satisfy a specific condition.
SELECT COUNT(column_name) FROM table_name WHERE condition;
COUNT can be used on a column or a wildcard (*) to count rows in a table.
-- Count the number of employees SELECT COUNT(EmployeeID) FROM Employees;
This query counts the total number of employees in the Employees table.
-- Count the number of employees with a salary greater than 50000 SELECT COUNT(EmployeeID) FROM Employees WHERE Salary > 50000;
This query counts the number of employees whose salary is greater than 50,000.
The SUM function is used to calculate the total sum of a numeric column. It is often used to sum up values such as sales, salary, or quantity.
SELECT SUM(column_name) FROM table_name WHERE condition;
SUM can only be used on numeric columns, and it returns the sum of the values in that column.
-- Calculate the total salary of all employees SELECT SUM(Salary) FROM Employees;
This query calculates the total salary of all employees in the Employees table.
-- Calculate the total salary of employees with a salary greater than 50000 SELECT SUM(Salary) FROM Employees WHERE Salary > 50000;
This query calculates the total salary of employees whose salary is greater than 50,000.
The AVG function is used to calculate the average value of a numeric column. It is useful when you need to find the mean value of a set of numbers.
SELECT AVG(column_name) FROM table_name WHERE condition;
AVG can only be used on numeric columns, and it returns the average of the values in that column.
-- Calculate the average salary of all employees SELECT AVG(Salary) FROM Employees;
This query calculates the average salary of all employees in the Employees table.
-- Calculate the average salary of employees who have been with the company for more than 5 years SELECT AVG(Salary) FROM Employees WHERE YearsAtCompany > 5;
This query calculates the average salary of employees who have been with the company for more than 5 years.
The MIN function is used to find the minimum value in a column. It is commonly used for finding the smallest value in a set of data, such as the lowest salary or the earliest date.
SELECT MIN(column_name) FROM table_name WHERE condition;
MIN can be used on any data type, including numeric, date, and text columns, and it returns the smallest value.
-- Find the lowest salary among all employees SELECT MIN(Salary) FROM Employees;
This query retrieves the lowest salary from the Employees table.
-- Find the earliest hire date of employees who have been with the company more than 10 years SELECT MIN(HireDate) FROM Employees WHERE YearsAtCompany > 10;
This query finds the earliest hire date of employees who have been with the company for more than 10 years.
The MAX function is used to find the maximum value in a column. It is commonly used for finding the highest value, such as the highest salary or the latest date.
SELECT MAX(column_name) FROM table_name WHERE condition;
MAX can be used on any data type, including numeric, date, and text columns, and it returns the largest value.
-- Find the highest salary among all employees SELECT MAX(Salary) FROM Employees;
This query retrieves the highest salary from the Employees table.
-- Find the most recent hire date of employees who have been with the company more than 5 years SELECT MAX(HireDate) FROM Employees WHERE YearsAtCompany > 5;
This query finds the most recent hire date of employees who have been with the company for more than 5 years.
SQL allows you to combine multiple aggregate functions in a single query. This can be useful when you want to perform different calculations on the same set of data.
-- Calculate the number of employees, total salary, and average salary SELECT COUNT(EmployeeID), SUM(Salary), AVG(Salary) FROM Employees;
This query calculates the total number of employees, the total salary, and the average salary in the Employees table in a single query.
Aggregate functions like COUNT, SUM, AVG, MIN, and MAX are essential tools in SQL for performing calculations on large sets of data. These functions allow you to easily summarize and analyze data in a table, and they can be used in combination with the GROUP BY clause to group the data before performing the aggregation. Understanding how to use these functions will help you write more efficient and powerful SQL queries.