Aggregate Functions, GROUP BY, HAVING, and Reporting Queries
Use MariaDB to summarize data for dashboards, reporting, analytics, and operational review.
Inside this chapter
- Why Aggregation Matters
- Common Aggregate Functions
- Grouping by Meaningful Dimensions
- Filtering Aggregates with HAVING
Series navigation
Study the chapters in order for the smoothest path from relational foundations to production-level MariaDB operations. Use the navigation at the bottom of each page to move chapter by chapter through the full series.
Why Aggregation Matters
Applications often need more than raw rows. Teams want totals, averages, maximum values, monthly counts, and grouped reports. MariaDB can calculate these efficiently close to the data, which reduces application-side loops and improves clarity.
Common Aggregate Functions
COUNT()counts rowsSUM()totals numeric valuesAVG()computes averagesMIN()andMAX()find boundaries
SELECT COUNT(*) AS total_customers
FROM customers
WHERE status = 'ACTIVE'; Grouping by Meaningful Dimensions
SELECT
order_status,
COUNT(*) AS order_count
FROM orders
GROUP BY order_status;
Grouping lets you summarize by status, department, region, product category, month, assignee, or any other useful dimension. This is the basis of many management dashboards.
Filtering Aggregates with HAVING
SELECT
customer_id,
COUNT(*) AS total_orders
FROM orders
GROUP BY customer_id
HAVING COUNT(*) >= 5;
WHERE filters rows before grouping. HAVING filters grouped results after aggregation. Understanding that distinction prevents many beginner mistakes.