Part 2 of 5 · 1 min read
Aggregate functions over a whole table
COUNT, SUM, AVG, MIN and MAX, computed across every row at once.
Think of a cashier totaling an entire day's receipts at once: one total count of transactions, one total sum of money taken, one average sale.
In plain words: an aggregate function computes a single value from many rows. COUNT counts rows. SUM adds a numeric column. AVG computes its average. MIN and MAX find the smallest and largest value. Without GROUP BY, an aggregate function computes one single value across the entire table.
A worked example. The total number of orders ever placed, and their total value.
The key lines.
SELECT COUNT(*) AS total_orders FROM orders;
SELECT SUM(price) AS total_revenue FROM order_items oi JOIN books b ON oi.book_id = b.book_id;
Aggregate function
An aggregate function computes a single value from a set of rows, such as a count, a sum, or an average. Without GROUP BY, it computes over the entire table at once.
Example: COUNT(*) against all of orders returns one number: the total row count.