You've learned how to retrieve specific rows and columns from your tables using SELECT
, how to narrow down those results with WHERE
, and how to arrange them with ORDER BY
. While looking at individual rows is useful, data analysis often requires understanding data at a higher level. Instead of asking "What was the price of this specific product?", you might need to ask, "What is the average price of all products?" or "How many orders were placed yesterday?".
This is where aggregate functions come into play. Think of them as special SQL functions designed to perform calculations across a set of rows, ultimately returning a single, summarized value. Instead of operating on each row individually, they consume multiple rows and produce one result.
Imagine you have a simple Orders
table:
OrderID | CustomerID | OrderTotal | OrderDate |
---|---|---|---|
1 | 101 | 55.00 | 2023-10-26 |
2 | 102 | 120.50 | 2023-10-26 |
3 | 101 | 75.00 | 2023-10-27 |
4 | 103 | 30.25 | 2023-10-27 |
5 | 102 | 90.75 | 2023-10-28 |
Using aggregate functions, you could answer questions like:
OrderTotal
column).OrderTotal
column).OrderTotal
).OrderTotal
).These functions are fundamental tools for summarizing data and extracting meaningful insights. They allow you to condense large amounts of detailed information into concise, informative summary statistics.
In the sections that follow, we will look at the most common and useful aggregate functions provided by SQL:
COUNT
: To count rows or values.SUM
: To add up values in a column.AVG
: To calculate the average of values in a column.MIN
: To find the minimum value in a column.MAX
: To find the maximum value in a column.We'll start by applying these functions to entire tables or result sets, and later in this chapter, you'll learn how to apply them to specific groups within your data using the GROUP BY
clause. For now, focus on understanding that aggregate functions provide a way to calculate a single summary value from multiple rows of data.
© 2025 ApX Machine Learning