sql
  1. sql-aggregate-functions

SQL Functions: Aggregate Functions

Aggregate functions in SQL are used to perform calculations on a set of values and return a single value. These functions can be used to calculate the sum, average, minimum and maximum values from a column.

Syntax

The syntax for an aggregate function in SQL is as follows:

FUNCTION_NAME(column_name)

Some common aggregate functions in SQL include:

  • SUM
  • AVG
  • MIN
  • MAX
  • COUNT

Example

Let's say we have a table named employees with columns name, age and salary. To find the average salary of all employees, we can use the AVG function.

SELECT AVG(salary) FROM employees;

Output

The output of the above query will be a single value representing the average salary of all employees.

+--------------+
| AVG(salary)  |
+--------------+
| 50000.00     |
+--------------+

Explanation

In the above query, the AVG function calculates the average salary from the salary column of the employees table.

Use

Aggregate functions are useful when we want to calculate summary statistics from large datasets. They are commonly used in reports and business intelligence applications.

Important Points

  • Aggregate functions can only be used with SELECT statements.
  • Most aggregate functions ignore NULL values, except for the COUNT function which includes NULL values in its calculation.
  • Aggregate functions can be combined with other functions and expressions in SELECT statements.

Summary

Aggregate functions in SQL are used to calculate summary statistics from a set of values. They can be used to calculate the sum, average, minimum and maximum values from a column. Aggregate functions can only be used with SELECT statements and are commonly used in reports and business intelligence applications.

Published on: