Multiple Aggregations in One Query
What you'll learn: How to calculate several summary statistics simultaneously in a single SELECT statement.
Now that you understand individual aggregate functions like COUNT, SUM, AVG, MIN, and MAX, you can combine them in one query to compute multiple statistics at once. This is both efficient and convenient—one database round-trip gives you a complete statistical snapshot.
The Core Idea
Instead of writing separate queries for each statistic you need, you can list multiple aggregate functions in your SELECT clause, separated by commas. Each function operates independently on the dataset and returns its own result column.
Think of it like ordering multiple items from a menu at once, rather than making separate trips to the counter for each dish. You get everything in one go.
Example Structure
SELECT
COUNT(*) AS total_orders,
SUM(amount) AS total_revenue,
AVG(amount) AS average_order,
MIN(amount) AS smallest_order,
MAX(amount) AS largest_order
FROM orders;
This single query computes five different statistics about your orders table simultaneously. Each aggregate function produces one value in the result set.
When Combined with GROUP BY
Multiple aggregations become even more powerful with GROUP BY. You can compute several statistics per group:
SELECT
customer_id,
COUNT(*) AS order_count,
SUM(amount) AS total_spent,
AVG(amount) AS avg_order_value
FROM orders
GROUP BY customer_id;
Now each customer gets their own row with all three statistics computed.
Why This Matters
Multiple aggregations reduce query complexity, improve performance (one database scan instead of many), and make your code cleaner and easier to maintain.
Key Takeaway: You can combine multiple aggregate functions in a single SELECT statement to compute several statistics at once, making your queries more efficient and comprehensive.