Aggregate Functions: SUM, AVG, COUNT, MIN, MAX & GROUP_CONCAT in MySQL
Overview & Learning Objectives
Data analysis begins with summarizing thousands of atomic records into meaningful business KPIs: total sales volume, average order value, unique customers, and category boundaries. MySQL provides core ANSI aggregate functions (`COUNT`, `SUM`, `AVG`, `MIN`, `MAX`) which collapse rows into scalar summaries while automatically ignoring NULL values. In addition, MySQL features the unique `GROUP_CONCAT()` function, allowing analysts to aggregate rows into readable comma-separated lists.
- Master the critical distinctions between `COUNT(*)`, `COUNT(col)`, and `COUNT(DISTINCT col)`.
- Compute executive revenue and average ticket metrics using `SUM()` and `AVG()`.
- Leverage MySQL's signature `GROUP_CONCAT()` to aggregate string values with custom sorting and delimiters.
Industry Standard
Standard: Analytics engineering teams at Swiggy and Zomato standardize financial reporting on `COUNT(DISTINCT user_id)` and `ROUND(AVG(bill_amount), 2)` to monitor daily customer retention and basket health.
Real-World Example: ScoopCraft Dark-Store Daily KPI Rollup
ScoopCraft operations computes daily summary metrics across 15,000 order transactions.
| Metric Name | SQL Expression | Result | Analytical Insight |
|---|---|---|---|
| Total Orders | COUNT(*) | 15,420 orders | Total delivery volume across all hubs |
| Unique Buyers | COUNT(DISTINCT customer_id) | 11,200 customers | Measures customer breadth vs repeat orders |
| Gross Sales | SUM(order_total) | ₹5,240,000.00 | Total top-line daily gross merchandise value |
| Average Ticket | ROUND(AVG(order_total), 2) | ₹339.82 | Average consumer spend per order |
Functions & Syntax Covered in this Chapter
Here is the definition, execution syntax, parameter breakdown, and real-world business example for each function/syntax covered in this chapter:
COUNT() / COUNT(DISTINCT)
Definition: Counts rows matching criteria. COUNT(*) counts all rows including NULLs; COUNT(col) counts non-NULL values; COUNT(DISTINCT col) counts unique values.
SELECT COUNT(*), COUNT(DISTINCT customer_id) FROM orders;
*: Counts every row satisfying the WHERE clause.DISTINCT col: Deduplicates values before counting, ignoring NULLs.
COUNT(order_id) AS total_orders, COUNT(DISTINCT customer_id) AS unique_buyersSUM() & AVG()
Definition: Calculates the arithmetic sum or average of a numeric column, automatically skipping NULL records.
SELECT SUM(order_total), ROUND(AVG(order_total), 2) FROM orders;
col: Numeric column (INT, DECIMAL, FLOAT).
SUM(order_total) AS gross_rev, AVG(order_total) AS aovMIN() & MAX()
Definition: Returns the minimum or maximum value in a set. Works on numeric, text (alphabetical), and temporal (oldest/newest date) columns.
SELECT MIN(order_date), MAX(order_date), MIN(price), MAX(price) FROM products;
col: Any comparable column type.
MIN(price) AS cheapest, MAX(price) AS most_expensiveGROUP_CONCAT() (MySQL Signature Aggregate)
Definition: Concatenates non-NULL values from multiple rows into a single comma-separated string per group with optional sorting and separators.
SELECT store_id, GROUP_CONCAT(flavor_name ORDER BY price DESC SEPARATOR " | ") FROM menu GROUP BY store_id;
DISTINCT: Optional flag eliminating duplicate string values.ORDER BY: Specifies ordering of elements within the concatenated result.SEPARATOR: String literal inserted between values (defaults to comma ",").
GROUP_CONCAT(flavor_name SEPARATOR ', ')Common Mistakes & Limitations
- Assuming `AVG(tip_amount)` includes orders with NULL tips; `AVG()` ignores NULLs, calculating the average tip only among customers who tipped.
- Exceeding MySQL's default `group_concat_max_len` (1024 bytes) when using `GROUP_CONCAT()` on large datasets, causing truncated strings.
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Execute SQL Query | Ctrl + Enter | Cmd + Enter |
Pro Tips & Modern Best Practices
- If you need `GROUP_CONCAT()` to output long strings without truncation, execute `SET SESSION group_concat_max_len = 1000000;` before running your query.
- Use `SUM(order_status = "Delivered")` in MySQL as a brilliant shorthand for conditional counting (since boolean expressions evaluate to 1 or 0!).