Aggregate functions in SQL are used to perform calculations on a set of values to return a single value. Here's how you can use each of the primary aggregate functions:
SUM: This function adds up all the values in a specified column. It is typically used with numeric data types.
SELECT SUM(salary) AS total_salary FROM employees;
This query will return the total sum of salaries in the employees
table.
AVG: This function calculates the average of values in a specified column. It is also used with numeric data types.
SELECT AVG(salary) AS average_salary FROM employees;
This will return the average salary of employees in the employees
table.
COUNT: This function returns the number of rows that match a specified condition. It can be used with any data type.
SELECT COUNT(*) AS total_employees FROM employees;
This query counts all rows in the employees
table.
MIN: This function returns the smallest value in a specified column. It can be used with numeric or date/time data types.
SELECT MIN(hire_date) AS earliest_hire FROM employees;
This will return the earliest hire date among all employees.
MAX: This function returns the largest value in a specified column. It can be used with numeric or date/time data types.
SELECT MAX(salary) AS highest_salary FROM employees;
This query will return the highest salary in the employees
table.
Each of these aggregate functions serves a unique purpose:
MIN
, it can be used with numeric or date/time values to find the maximum amount or latest date.Each function is designed to answer different types of questions about your data set, from total values and averages to counts and extreme values.
You can combine multiple aggregate functions in a single SQL query by listing them in the SELECT
statement. Here's an example that combines SUM
, AVG
, COUNT
, MIN
, and MAX
:
SELECT SUM(salary) AS total_salary, AVG(salary) AS average_salary, COUNT(*) AS total_employees, MIN(hire_date) AS earliest_hire, MAX(hire_date) AS latest_hire FROM employees;
This query will return multiple summary statistics in a single result set. Each column in the result will represent the result of a different aggregate function applied to the employees
table.
For calculating totals, you should use the SUM
function. This function is specifically designed to add up all values in a specified numeric column. For example, to calculate the total sales from a sales table, you would use:
SELECT SUM(sales_amount) AS total_sales FROM sales;
For calculating averages, you should use the AVG
function. This function calculates the mean of the values in a specified numeric column. For example, to calculate the average sales amount, you would use:
SELECT AVG(sales_amount) AS average_sales FROM sales;
Both SUM
and AVG
are crucial for analyzing numerical data, with SUM
focusing on the total value and AVG
providing insight into the typical value within a set.
The above is the detailed content of How do I use aggregate functions in SQL to summarize data (SUM, AVG, COUNT, MIN, MAX)?. For more information, please follow other related articles on the PHP Chinese website!