SQL Functions - Aggregate Functions

Introduction

The aggregate functions in SQL provide ways to summarize data, calculating the total, average, minimum, maximum, etc. of a set of values. These functions handle data in different ways, allowing for calculations to be performed efficiently.

Functions

Here are some of the key aggregate functions:

  • SUM(): Calculates the sum of all values in a set.
  • AVG(): Calculates the average (mean) of values in a set.
  • MIN(): Finds the smallest value in a set.
  • MAX(): Finds the largest value in a set.
  • COUNT(): Counts the number of values in a set.
  • DISTINCT(): Returns only the unique values in a set.
  • GROUP BY(): Groups rows based on one or more columns, allowing calculations on each group.
Example Usage (Simplified)

For example, to find the total sales, you would use the following:

SELECT SUM(amount) FROM orders;

Further Resources

Learn more about aggregate functions: SQL Server Documentation